blob: d66e2cbd191e381ef9753360c1d98f5f18564604 [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 Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020021# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020022#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020027static const char *vim_special_path = "_vim_path_";
28
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020029#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020030#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020031#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020032#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
33#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
34
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020040 N_("empty keys are not allowed"))
41#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
42#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
43#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
44#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
45#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
46#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020051 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020055#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020056
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020067static PyObject *WindowNew(win_T *, tabpage_T *);
68static PyObject *BufferNew (buf_T *);
69static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020070
71static PyInt RangeStart;
72static PyInt RangeEnd;
73
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020074static PyObject *globals;
75
Bram Moolenaarf4258302013-06-02 18:20:17 +020076static PyObject *py_chdir;
77static PyObject *py_fchdir;
78static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020079static PyObject *vim_module;
80static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020081
Bram Moolenaar81c40c52013-06-12 14:41:04 +020082static PyObject *py_find_module;
83static PyObject *py_load_module;
84
85static PyObject *VimError;
86
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020087/*
88 * obtain a lock on the Vim data structures
89 */
90 static void
91Python_Lock_Vim(void)
92{
93}
94
95/*
96 * release a lock on the Vim data structures
97 */
98 static void
99Python_Release_Vim(void)
100{
101}
102
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200103/*
104 * The "todecref" argument holds a pointer to PyObject * that must be
105 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
106 * was needed to generate returned value is object.
107 *
108 * Use Py_XDECREF to decrement reference count.
109 */
110 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200111StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200113 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200114
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200115 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116 {
117
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
119 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120 return NULL;
121
122 *todecref = NULL;
123 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200126 PyObject *bytes;
127
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200128 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200131 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
132 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200133 {
134 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200135 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200136 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137
138 *todecref = bytes;
139 }
140 else
141 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200142#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200143 PyErr_FORMAT(PyExc_TypeError,
144 N_("expected str() or unicode() instance, but got %s"),
145 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200146#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200147 PyErr_FORMAT(PyExc_TypeError,
148 N_("expected bytes() or str() instance, but got %s"),
149 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200151 return NULL;
152 }
153
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200154 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200155}
156
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200157#define NUMBER_LONG 1
158#define NUMBER_INT 2
159#define NUMBER_NATURAL 4
160#define NUMBER_UNSIGNED 8
161
162 static int
163NumberToLong(PyObject *obj, long *result, int flags)
164{
165#if PY_MAJOR_VERSION < 3
166 if (PyInt_Check(obj))
167 {
168 *result = PyInt_AsLong(obj);
169 if (PyErr_Occurred())
170 return -1;
171 }
172 else
173#endif
174 if (PyLong_Check(obj))
175 {
176 *result = PyLong_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else if (PyNumber_Check(obj))
181 {
182 PyObject *num;
183
184 if (!(num = PyNumber_Long(obj)))
185 return -1;
186
187 *result = PyLong_AsLong(num);
188
189 Py_DECREF(num);
190
191 if (PyErr_Occurred())
192 return -1;
193 }
194 else
195 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200196#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200197 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200198 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200199 "coercing to long(), but got %s"),
200 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200201#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200202 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200203 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 "but got %s"),
205 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200206#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200207 return -1;
208 }
209
210 if (flags & NUMBER_INT)
211 {
212 if (*result > INT_MAX)
213 {
214 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200215 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218 else if (*result < INT_MIN)
219 {
220 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200221 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200222 return -1;
223 }
224 }
225
226 if (flags & NUMBER_NATURAL)
227 {
228 if (*result <= 0)
229 {
230 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200231 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200232 return -1;
233 }
234 }
235 else if (flags & NUMBER_UNSIGNED)
236 {
237 if (*result < 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200240 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244
245 return 0;
246}
247
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200248 static int
249add_string(PyObject *list, char *s)
250{
251 PyObject *string;
252
253 if (!(string = PyString_FromString(s)))
254 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 if (PyList_Append(list, string))
257 {
258 Py_DECREF(string);
259 return -1;
260 }
261
262 Py_DECREF(string);
263 return 0;
264}
265
266 static PyObject *
267ObjectDir(PyObject *self, char **attributes)
268{
269 PyMethodDef *method;
270 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200271 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200272
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200273 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200274 return NULL;
275
276 if (self)
277 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200278 if (add_string(ret, (char *) method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200279 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281 return NULL;
282 }
283
284 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200285 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200286 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200287 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 return NULL;
289 }
290
291#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200292 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200293 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 return NULL;
296 }
297#endif
298
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200299 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200300}
301
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200302/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200303 */
304
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200305/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200306typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200307
308static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309
310typedef struct
311{
312 PyObject_HEAD
313 long softspace;
314 long error;
315} OutputObject;
316
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200317static char *OutputAttrs[] = {
318 "softspace",
319 NULL
320};
321
322 static PyObject *
323OutputDir(PyObject *self)
324{
325 return ObjectDir(self, OutputAttrs);
326}
327
Bram Moolenaar77045652012-09-21 13:46:06 +0200328 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200329OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200330{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200331 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200332 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200333 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200334 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200335 return -1;
336 }
337
338 if (strcmp(name, "softspace") == 0)
339 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200342 return 0;
343 }
344
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200345 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return -1;
347}
348
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200349/* Buffer IO, we write one whole line at a time. */
350static garray_T io_ga = {0, 0, 1, 80, NULL};
351static writefn old_fn = NULL;
352
353 static void
354PythonIO_Flush(void)
355{
356 if (old_fn != NULL && io_ga.ga_len > 0)
357 {
358 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
359 old_fn((char_u *)io_ga.ga_data);
360 }
361 io_ga.ga_len = 0;
362}
363
364 static void
365writer(writefn fn, char_u *str, PyInt n)
366{
367 char_u *ptr;
368
369 /* Flush when switching output function. */
370 if (fn != old_fn)
371 PythonIO_Flush();
372 old_fn = fn;
373
374 /* Write each NL separated line. Text after the last NL is kept for
375 * writing later. */
376 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
377 {
378 PyInt len = ptr - str;
379
380 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
381 break;
382
383 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
384 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
385 fn((char_u *)io_ga.ga_data);
386 str = ptr + 1;
387 n -= len + 1;
388 io_ga.ga_len = 0;
389 }
390
391 /* Put the remaining text into io_ga for later printing. */
392 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
393 {
394 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
395 io_ga.ga_len += (int)n;
396 }
397}
398
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200399 static int
400write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200401{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200402 Py_ssize_t len = 0;
403 char *str = NULL;
404 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200405
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200406 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
407 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200408
409 Py_BEGIN_ALLOW_THREADS
410 Python_Lock_Vim();
411 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
412 Python_Release_Vim();
413 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200414 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200415
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200416 return 0;
417}
418
419 static PyObject *
420OutputWrite(OutputObject *self, PyObject *string)
421{
422 if (write_output(self, string))
423 return NULL;
424
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200425 Py_INCREF(Py_None);
426 return Py_None;
427}
428
429 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200430OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200431{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200432 PyObject *iterator;
433 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200435 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200437
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200438 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200440 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200441 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200442 Py_DECREF(iterator);
443 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
445 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 }
448
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 Py_DECREF(iterator);
450
451 /* Iterator may have finished due to an exception */
452 if (PyErr_Occurred())
453 return NULL;
454
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 Py_INCREF(Py_None);
456 return Py_None;
457}
458
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100459 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200460OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100461{
462 /* do nothing */
463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200467/***************/
468
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200469static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200470 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200471 {"write", (PyCFunction)OutputWrite, METH_O, ""},
472 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200473 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200474 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200475 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200476};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200477
478static OutputObject Output =
479{
480 PyObject_HEAD_INIT(&OutputType)
481 0,
482 0
483};
484
485static OutputObject Error =
486{
487 PyObject_HEAD_INIT(&OutputType)
488 0,
489 1
490};
491
492 static int
493PythonIO_Init_io(void)
494{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200495 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
496 return -1;
497 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
498 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200499
500 if (PyErr_Occurred())
501 {
502 EMSG(_("E264: Python: Error initialising I/O objects"));
503 return -1;
504 }
505
506 return 0;
507}
508
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200509typedef struct
510{
511 PyObject_HEAD
512 PyObject *module;
513} LoaderObject;
514static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200515
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200516 static void
517LoaderDestructor(LoaderObject *self)
518{
519 Py_DECREF(self->module);
520 DESTRUCTOR_FINISH(self);
521}
522
523 static PyObject *
524LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
525{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200526 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200527
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200528 Py_INCREF(ret);
529 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200530}
531
532static struct PyMethodDef LoaderMethods[] = {
533 /* name, function, calling, doc */
534 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
535 { NULL, NULL, 0, NULL}
536};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200537
538/* Check to see whether a Vim error has been reported, or a keyboard
539 * interrupt has been detected.
540 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200541
542 static void
543VimTryStart(void)
544{
545 ++trylevel;
546}
547
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200548 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549VimTryEnd(void)
550{
551 --trylevel;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200552 /* Without this it stops processing all subsequent VimL commands and
553 * generates strange error messages if I e.g. try calling Test() in a cycle */
554 did_emsg = FALSE;
555 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200556 if (got_int)
557 {
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200558 did_throw = got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200559 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200560 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200561 }
562 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 return (PyErr_Occurred() ? -1 : 0);
564 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200566 {
567 did_throw = FALSE;
568 return -1;
569 }
570 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200571 else
572 {
573 PyErr_SetVim((char *) current_exception->value);
574 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200575 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200576 }
577}
578
579 static int
580VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200581{
582 if (got_int)
583 {
584 PyErr_SetNone(PyExc_KeyboardInterrupt);
585 return 1;
586 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200587 return 0;
588}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589
590/* Vim module - Implementation
591 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200592
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200594VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200595{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200596 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200597 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200598 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200599
Bram Moolenaar389a1792013-06-23 13:00:44 +0200600 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601 return NULL;
602
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200603 Py_BEGIN_ALLOW_THREADS
604 Python_Lock_Vim();
605
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200606 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200607 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200608 update_screen(VALID);
609
610 Python_Release_Vim();
611 Py_END_ALLOW_THREADS
612
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200613 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200614 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200616 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200618 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200619 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200620 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621}
622
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200623/*
624 * Function to translate a typval_T into a PyObject; this will recursively
625 * translate lists/dictionaries into their Python equivalents.
626 *
627 * The depth parameter is to avoid infinite recursion, set it to 1 when
628 * you call VimToPython.
629 */
630 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200631VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200633 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200634 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200635 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636
637 /* Avoid infinite recursion */
638 if (depth > 100)
639 {
640 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200641 ret = Py_None;
642 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200643 }
644
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200645 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 * then and we can use it again. */
647 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
648 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
649 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200650 sprintf(ptrBuf, "%p",
651 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
652 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200653
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200654 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200655 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200656 Py_INCREF(ret);
657 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200658 }
659 }
660
661 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200662 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200663 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664 else if (our_tv->v_type == VAR_NUMBER)
665 {
666 char buf[NUMBUFLEN];
667
668 /* For backwards compatibility numbers are stored as strings. */
669 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200670 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200671 }
672# ifdef FEAT_FLOAT
673 else if (our_tv->v_type == VAR_FLOAT)
674 {
675 char buf[NUMBUFLEN];
676
677 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200678 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 }
680# endif
681 else if (our_tv->v_type == VAR_LIST)
682 {
683 list_T *list = our_tv->vval.v_list;
684 listitem_T *curr;
685
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686 if (list == NULL)
687 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200690 return NULL;
691
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200692 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200694 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200695 return NULL;
696 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200698 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
699 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200700 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200702 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200703 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200705 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200706 {
707 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200709 return NULL;
710 }
711 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 }
713 }
714 else if (our_tv->v_type == VAR_DICT)
715 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200717 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
718 long_u todo = ht->ht_used;
719 hashitem_T *hi;
720 dictitem_T *di;
721 if (our_tv->vval.v_dict == NULL)
722 return NULL;
723
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200724 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200725 return NULL;
726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200729 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200730 return NULL;
731 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200732
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 for (hi = ht->ht_array; todo > 0; ++hi)
734 {
735 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200737 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200738
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200740 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200741 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200742 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200744 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200747 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200748 Py_DECREF(newObj);
749 return NULL;
750 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200751 }
752 }
753 }
754 else
755 {
756 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 }
759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762
763 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200764VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200766 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200767 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200768 PyObject *string;
769 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200770 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200771 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772
Bram Moolenaar389a1792013-06-23 13:00:44 +0200773 if (!PyArg_ParseTuple(args, "O", &string))
774 return NULL;
775
776 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 return NULL;
778
779 Py_BEGIN_ALLOW_THREADS
780 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200781 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200782 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783 Python_Release_Vim();
784 Py_END_ALLOW_THREADS
785
Bram Moolenaar389a1792013-06-23 13:00:44 +0200786 Py_XDECREF(todecref);
787
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200788 if (VimTryEnd())
789 return NULL;
790
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 if (our_tv == NULL)
792 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200793 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 return NULL;
795 }
796
797 /* Convert the Vim type into a Python type. Create a dictionary that's
798 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200799 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200800 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200801 else
802 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200804 Py_DECREF(lookup_dict);
805 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806
807
808 Py_BEGIN_ALLOW_THREADS
809 Python_Lock_Vim();
810 free_tv(our_tv);
811 Python_Release_Vim();
812 Py_END_ALLOW_THREADS
813
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200814 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200815}
816
Bram Moolenaardb913952012-06-29 12:54:53 +0200817static PyObject *ConvertToPyObject(typval_T *);
818
819 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200820VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200821{
Bram Moolenaardb913952012-06-29 12:54:53 +0200822 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200823 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200824 char_u *expr;
825 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200826
Bram Moolenaar389a1792013-06-23 13:00:44 +0200827 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200828 return NULL;
829
830 Py_BEGIN_ALLOW_THREADS
831 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200832 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200833 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 Python_Release_Vim();
835 Py_END_ALLOW_THREADS
836
Bram Moolenaar389a1792013-06-23 13:00:44 +0200837 Py_XDECREF(todecref);
838
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200839 if (VimTryEnd())
840 return NULL;
841
Bram Moolenaardb913952012-06-29 12:54:53 +0200842 if (our_tv == NULL)
843 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200844 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200845 return NULL;
846 }
847
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200848 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200849 Py_BEGIN_ALLOW_THREADS
850 Python_Lock_Vim();
851 free_tv(our_tv);
852 Python_Release_Vim();
853 Py_END_ALLOW_THREADS
854
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200855 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200856}
857
858 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200859VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200860{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200861 char_u *str;
862 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200863 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200864
Bram Moolenaar389a1792013-06-23 13:00:44 +0200865 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200866 return NULL;
867
Bram Moolenaara54bf402012-12-05 16:30:07 +0100868#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200869 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100870#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200871 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100872#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200873
874 Py_XDECREF(todecref);
875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200876 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200877}
878
Bram Moolenaarf4258302013-06-02 18:20:17 +0200879 static PyObject *
880_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
881{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200882 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200883 PyObject *newwd;
884 PyObject *todecref;
885 char_u *new_dir;
886
Bram Moolenaard4209d22013-06-05 20:34:15 +0200887 if (_chdir == NULL)
888 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200889 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200890 return NULL;
891
892 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
893 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200894 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200895 return NULL;
896 }
897
898 if (!(new_dir = StringToChars(newwd, &todecref)))
899 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200900 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200901 Py_DECREF(newwd);
902 return NULL;
903 }
904
905 VimTryStart();
906
907 if (vim_chdir(new_dir))
908 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200910 Py_DECREF(newwd);
911 Py_XDECREF(todecref);
912
913 if (VimTryEnd())
914 return NULL;
915
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200916 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200917 return NULL;
918 }
919
920 Py_DECREF(newwd);
921 Py_XDECREF(todecref);
922
923 post_chdir(FALSE);
924
925 if (VimTryEnd())
926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929 }
930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200931 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200932}
933
934 static PyObject *
935VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
936{
937 return _VimChdir(py_chdir, args, kwargs);
938}
939
940 static PyObject *
941VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
942{
943 return _VimChdir(py_fchdir, args, kwargs);
944}
945
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200946typedef struct {
947 PyObject *callable;
948 PyObject *result;
949} map_rtp_data;
950
951 static void
952map_rtp_callback(char_u *path, void *_data)
953{
954 void **data = (void **) _data;
955 PyObject *pathObject;
956 map_rtp_data *mr_data = *((map_rtp_data **) data);
957
958 if (!(pathObject = PyString_FromString((char *) path)))
959 {
960 *data = NULL;
961 return;
962 }
963
964 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
965 pathObject, NULL);
966
967 Py_DECREF(pathObject);
968
969 if (!mr_data->result || mr_data->result != Py_None)
970 *data = NULL;
971 else
972 {
973 Py_DECREF(mr_data->result);
974 mr_data->result = NULL;
975 }
976}
977
978 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200980{
981 map_rtp_data data;
982
Bram Moolenaar389a1792013-06-23 13:00:44 +0200983 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200984 data.result = NULL;
985
986 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
987
988 if (data.result == NULL)
989 {
990 if (PyErr_Occurred())
991 return NULL;
992 else
993 {
994 Py_INCREF(Py_None);
995 return Py_None;
996 }
997 }
998 return data.result;
999}
1000
1001/*
1002 * _vim_runtimepath_ special path implementation.
1003 */
1004
1005 static void
1006map_finder_callback(char_u *path, void *_data)
1007{
1008 void **data = (void **) _data;
1009 PyObject *list = *((PyObject **) data);
1010 PyObject *pathObject1, *pathObject2;
1011 char *pathbuf;
1012 size_t pathlen;
1013
1014 pathlen = STRLEN(path);
1015
1016#if PY_MAJOR_VERSION < 3
1017# define PY_MAIN_DIR_STRING "python2"
1018#else
1019# define PY_MAIN_DIR_STRING "python3"
1020#endif
1021#define PY_ALTERNATE_DIR_STRING "pythonx"
1022
1023#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1024 if (!(pathbuf = PyMem_New(char,
1025 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1026 {
1027 PyErr_NoMemory();
1028 *data = NULL;
1029 return;
1030 }
1031
1032 mch_memmove(pathbuf, path, pathlen + 1);
1033 add_pathsep((char_u *) pathbuf);
1034
1035 pathlen = STRLEN(pathbuf);
1036 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1037 PYTHONX_STRING_LENGTH + 1);
1038
1039 if (!(pathObject1 = PyString_FromString(pathbuf)))
1040 {
1041 *data = NULL;
1042 PyMem_Free(pathbuf);
1043 return;
1044 }
1045
1046 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1047 PYTHONX_STRING_LENGTH + 1);
1048
1049 if (!(pathObject2 = PyString_FromString(pathbuf)))
1050 {
1051 Py_DECREF(pathObject1);
1052 PyMem_Free(pathbuf);
1053 *data = NULL;
1054 return;
1055 }
1056
1057 PyMem_Free(pathbuf);
1058
1059 if (PyList_Append(list, pathObject1)
1060 || PyList_Append(list, pathObject2))
1061 *data = NULL;
1062
1063 Py_DECREF(pathObject1);
1064 Py_DECREF(pathObject2);
1065}
1066
1067 static PyObject *
1068Vim_GetPaths(PyObject *self UNUSED)
1069{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001070 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001071
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001072 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001073 return NULL;
1074
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001075 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001076
1077 if (PyErr_Occurred())
1078 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001079 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001080 return NULL;
1081 }
1082
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001083 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001084}
1085
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001086 static PyObject *
1087call_load_module(char *name, int len, PyObject *find_module_result)
1088{
1089 PyObject *fd, *pathname, *description;
1090
Bram Moolenaarc476e522013-06-23 13:46:40 +02001091 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001092 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001093 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001094 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001095 Py_TYPE_NAME(find_module_result));
1096 return NULL;
1097 }
1098 if (PyTuple_GET_SIZE(find_module_result) != 3)
1099 {
1100 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001101 N_("expected 3-tuple as imp.find_module() result, but got "
1102 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001103 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001104 return NULL;
1105 }
1106
1107 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1108 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1109 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1110 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001111 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001112 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001113 return NULL;
1114 }
1115
1116 return PyObject_CallFunction(py_load_module,
1117 "s#OOO", name, len, fd, pathname, description);
1118}
1119
1120 static PyObject *
1121find_module(char *fullname, char *tail, PyObject *new_path)
1122{
1123 PyObject *find_module_result;
1124 PyObject *module;
1125 char *dot;
1126
1127 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
1128 {
1129 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001130 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001131 * first component
1132 */
1133 PyObject *newest_path;
1134 int partlen = (int) (dot - 1 - tail);
1135
1136 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1137 "s#O", tail, partlen, new_path)))
1138 return NULL;
1139
1140 if (!(module = call_load_module(
1141 fullname,
1142 ((int) (tail - fullname)) + partlen,
1143 find_module_result)))
1144 {
1145 Py_DECREF(find_module_result);
1146 return NULL;
1147 }
1148
1149 Py_DECREF(find_module_result);
1150
1151 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1152 {
1153 Py_DECREF(module);
1154 return NULL;
1155 }
1156
1157 Py_DECREF(module);
1158
1159 module = find_module(fullname, dot + 1, newest_path);
1160
1161 Py_DECREF(newest_path);
1162
1163 return module;
1164 }
1165 else
1166 {
1167 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1168 "sO", tail, new_path)))
1169 return NULL;
1170
1171 if (!(module = call_load_module(
1172 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001173 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001174 find_module_result)))
1175 {
1176 Py_DECREF(find_module_result);
1177 return NULL;
1178 }
1179
1180 Py_DECREF(find_module_result);
1181
1182 return module;
1183 }
1184}
1185
1186 static PyObject *
1187FinderFindModule(PyObject *self, PyObject *args)
1188{
1189 char *fullname;
1190 PyObject *module;
1191 PyObject *new_path;
1192 LoaderObject *loader;
1193
1194 if (!PyArg_ParseTuple(args, "s", &fullname))
1195 return NULL;
1196
1197 if (!(new_path = Vim_GetPaths(self)))
1198 return NULL;
1199
1200 module = find_module(fullname, fullname, new_path);
1201
1202 Py_DECREF(new_path);
1203
1204 if (!module)
1205 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001206 if (PyErr_Occurred())
1207 {
1208 if (PyErr_ExceptionMatches(PyExc_ImportError))
1209 PyErr_Clear();
1210 else
1211 return NULL;
1212 }
1213
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001214 Py_INCREF(Py_None);
1215 return Py_None;
1216 }
1217
1218 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1219 {
1220 Py_DECREF(module);
1221 return NULL;
1222 }
1223
1224 loader->module = module;
1225
1226 return (PyObject *) loader;
1227}
1228
1229 static PyObject *
1230VimPathHook(PyObject *self UNUSED, PyObject *args)
1231{
1232 char *path;
1233
1234 if (PyArg_ParseTuple(args, "s", &path)
1235 && STRCMP(path, vim_special_path) == 0)
1236 {
1237 Py_INCREF(vim_module);
1238 return vim_module;
1239 }
1240
1241 PyErr_Clear();
1242 PyErr_SetNone(PyExc_ImportError);
1243 return NULL;
1244}
1245
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001246/*
1247 * Vim module - Definitions
1248 */
1249
1250static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001251 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001252 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001253 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001254 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1255 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001256 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1257 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001258 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001259 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001260 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1261 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1262 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001263};
1264
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001265/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001266 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001267 */
1268
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001269static PyTypeObject IterType;
1270
1271typedef PyObject *(*nextfun)(void **);
1272typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001273typedef int (*traversefun)(void *, visitproc, void *);
1274typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001275
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001276/* Main purpose of this object is removing the need for do python
1277 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1278 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001279
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001280typedef struct
1281{
1282 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001283 void *cur;
1284 nextfun next;
1285 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001286 traversefun traverse;
1287 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001288} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001289
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001290 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001291IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1292 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001293{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001294 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001295
Bram Moolenaar774267b2013-05-21 20:51:59 +02001296 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001297 self->cur = start;
1298 self->next = next;
1299 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001300 self->traverse = traverse;
1301 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001302
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001303 return (PyObject *)(self);
1304}
1305
1306 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001307IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001308{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001309 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001310 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001311 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001312}
1313
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001314 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001315IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001316{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317 if (self->traverse != NULL)
1318 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319 else
1320 return 0;
1321}
1322
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001323/* Mac OSX defines clear() somewhere. */
1324#ifdef clear
1325# undef clear
1326#endif
1327
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001328 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001329IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001330{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001331 if (self->clear != NULL)
1332 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333 else
1334 return 0;
1335}
1336
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001337 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001338IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001339{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001340 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001341}
1342
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001343 static PyObject *
1344IterIter(PyObject *self)
1345{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001346 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001347 return self;
1348}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001349
Bram Moolenaardb913952012-06-29 12:54:53 +02001350typedef struct pylinkedlist_S {
1351 struct pylinkedlist_S *pll_next;
1352 struct pylinkedlist_S *pll_prev;
1353 PyObject *pll_obj;
1354} pylinkedlist_T;
1355
1356static pylinkedlist_T *lastdict = NULL;
1357static pylinkedlist_T *lastlist = NULL;
1358
1359 static void
1360pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1361{
1362 if (ref->pll_prev == NULL)
1363 {
1364 if (ref->pll_next == NULL)
1365 {
1366 *last = NULL;
1367 return;
1368 }
1369 }
1370 else
1371 ref->pll_prev->pll_next = ref->pll_next;
1372
1373 if (ref->pll_next == NULL)
1374 *last = ref->pll_prev;
1375 else
1376 ref->pll_next->pll_prev = ref->pll_prev;
1377}
1378
1379 static void
1380pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1381{
1382 if (*last == NULL)
1383 ref->pll_prev = NULL;
1384 else
1385 {
1386 (*last)->pll_next = ref;
1387 ref->pll_prev = *last;
1388 }
1389 ref->pll_next = NULL;
1390 ref->pll_obj = self;
1391 *last = ref;
1392}
1393
1394static PyTypeObject DictionaryType;
1395
1396typedef struct
1397{
1398 PyObject_HEAD
1399 dict_T *dict;
1400 pylinkedlist_T ref;
1401} DictionaryObject;
1402
Bram Moolenaara9922d62013-05-30 13:01:18 +02001403static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1404
1405#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1406
Bram Moolenaardb913952012-06-29 12:54:53 +02001407 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001408DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001409{
1410 DictionaryObject *self;
1411
Bram Moolenaara9922d62013-05-30 13:01:18 +02001412 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001413 if (self == NULL)
1414 return NULL;
1415 self->dict = dict;
1416 ++dict->dv_refcount;
1417
1418 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1419
1420 return (PyObject *)(self);
1421}
1422
Bram Moolenaara9922d62013-05-30 13:01:18 +02001423 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001424py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001425{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001426 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001427
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001428 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001429 {
1430 PyErr_NoMemory();
1431 return NULL;
1432 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001433 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001434
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001435 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001436}
1437
1438 static PyObject *
1439DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1440{
1441 DictionaryObject *self;
1442 dict_T *dict;
1443
1444 if (!(dict = py_dict_alloc()))
1445 return NULL;
1446
1447 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1448
1449 --dict->dv_refcount;
1450
1451 if (kwargs || PyTuple_Size(args))
1452 {
1453 PyObject *tmp;
1454 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1455 {
1456 Py_DECREF(self);
1457 return NULL;
1458 }
1459
1460 Py_DECREF(tmp);
1461 }
1462
1463 return (PyObject *)(self);
1464}
1465
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001466 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001467DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001468{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001469 pyll_remove(&self->ref, &lastdict);
1470 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001471
1472 DESTRUCTOR_FINISH(self);
1473}
1474
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001475static char *DictionaryAttrs[] = {
1476 "locked", "scope",
1477 NULL
1478};
1479
1480 static PyObject *
1481DictionaryDir(PyObject *self)
1482{
1483 return ObjectDir(self, DictionaryAttrs);
1484}
1485
Bram Moolenaardb913952012-06-29 12:54:53 +02001486 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001487DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001488{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001489 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001490 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001491 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001492 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001493 return -1;
1494 }
1495
1496 if (strcmp(name, "locked") == 0)
1497 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001498 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001499 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001500 PyErr_SET_STRING(PyExc_TypeError,
1501 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001502 return -1;
1503 }
1504 else
1505 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001506 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001507 if (istrue == -1)
1508 return -1;
1509 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001510 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001511 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001512 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001513 }
1514 return 0;
1515 }
1516 else
1517 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001518 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001519 return -1;
1520 }
1521}
1522
1523 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001524DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001525{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001526 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001527}
1528
Bram Moolenaara9922d62013-05-30 13:01:18 +02001529#define DICT_FLAG_HAS_DEFAULT 0x01
1530#define DICT_FLAG_POP 0x02
1531#define DICT_FLAG_NONE_DEFAULT 0x04
1532#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1533#define DICT_FLAG_RETURN_PAIR 0x10
1534
Bram Moolenaardb913952012-06-29 12:54:53 +02001535 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001536_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001537{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001538 PyObject *keyObject;
1539 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001540 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001541 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001542 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001543 dict_T *dict = self->dict;
1544 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001545 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001546
Bram Moolenaara9922d62013-05-30 13:01:18 +02001547 if (flags & DICT_FLAG_HAS_DEFAULT)
1548 {
1549 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1550 return NULL;
1551 }
1552 else
1553 keyObject = args;
1554
1555 if (flags & DICT_FLAG_RETURN_BOOL)
1556 defObject = Py_False;
1557
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001558 if (!(key = StringToChars(keyObject, &todecref)))
1559 return NULL;
1560
1561 if (*key == NUL)
1562 {
1563 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001564 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001565 return NULL;
1566 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001567
Bram Moolenaara9922d62013-05-30 13:01:18 +02001568 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001569
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001570 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001571
Bram Moolenaara9922d62013-05-30 13:01:18 +02001572 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001573 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001574 if (defObject)
1575 {
1576 Py_INCREF(defObject);
1577 return defObject;
1578 }
1579 else
1580 {
1581 PyErr_SetObject(PyExc_KeyError, keyObject);
1582 return NULL;
1583 }
1584 }
1585 else if (flags & DICT_FLAG_RETURN_BOOL)
1586 {
1587 Py_INCREF(Py_True);
1588 return Py_True;
1589 }
1590
1591 di = dict_lookup(hi);
1592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594 return NULL;
1595
1596 if (flags & DICT_FLAG_POP)
1597 {
1598 if (dict->dv_lock)
1599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001600 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001601 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001602 return NULL;
1603 }
1604
1605 hash_remove(&dict->dv_hashtab, hi);
1606 dictitem_free(di);
1607 }
1608
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001609 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610}
1611
1612 static PyObject *
1613DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1614{
1615 return _DictionaryItem(self, keyObject, 0);
1616}
1617
1618 static int
1619DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1620{
1621 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001622 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001624 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001625
Bram Moolenaardee2e312013-06-23 16:35:47 +02001626 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001628 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001629}
1630
1631typedef struct
1632{
1633 hashitem_T *ht_array;
1634 long_u ht_used;
1635 hashtab_T *ht;
1636 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001637 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001638} dictiterinfo_T;
1639
1640 static PyObject *
1641DictionaryIterNext(dictiterinfo_T **dii)
1642{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644
1645 if (!(*dii)->todo)
1646 return NULL;
1647
1648 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1649 (*dii)->ht->ht_used != (*dii)->ht_used)
1650 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001651 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001652 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001653 return NULL;
1654 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001655
Bram Moolenaara9922d62013-05-30 13:01:18 +02001656 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1657 ++((*dii)->hi);
1658
1659 --((*dii)->todo);
1660
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001661 if (!(ret = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662 return NULL;
1663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665}
1666
1667 static PyObject *
1668DictionaryIter(DictionaryObject *self)
1669{
1670 dictiterinfo_T *dii;
1671 hashtab_T *ht;
1672
1673 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1674 {
1675 PyErr_NoMemory();
1676 return NULL;
1677 }
1678
1679 ht = &self->dict->dv_hashtab;
1680 dii->ht_array = ht->ht_array;
1681 dii->ht_used = ht->ht_used;
1682 dii->ht = ht;
1683 dii->hi = dii->ht_array;
1684 dii->todo = dii->ht_used;
1685
1686 return IterNew(dii,
1687 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1688 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001689}
1690
1691 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001692DictionaryAssItem(
1693 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001694{
1695 char_u *key;
1696 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001697 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001698 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001699 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001700
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001701 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001702 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001703 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001704 return -1;
1705 }
1706
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001707 if (!(key = StringToChars(keyObject, &todecref)))
1708 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001709
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001710 if (*key == NUL)
1711 {
1712 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001713 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001714 return -1;
1715 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001716
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001717 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001718
1719 if (valObject == NULL)
1720 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001721 hashitem_T *hi;
1722
Bram Moolenaardb913952012-06-29 12:54:53 +02001723 if (di == NULL)
1724 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001725 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001726 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001727 return -1;
1728 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001729 hi = hash_find(&dict->dv_hashtab, di->di_key);
1730 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001731 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001732 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001733 return 0;
1734 }
1735
1736 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001737 {
1738 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001740 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001741
1742 if (di == NULL)
1743 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001746 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001747 PyErr_NoMemory();
1748 return -1;
1749 }
1750 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001751 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001752
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001753 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 {
1755 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001756 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001757 RAISE_KEY_ADD_FAIL(key);
1758 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001759 return -1;
1760 }
1761 }
1762 else
1763 clear_tv(&di->di_tv);
1764
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001765 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001766
1767 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001768 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 return 0;
1770}
1771
Bram Moolenaara9922d62013-05-30 13:01:18 +02001772typedef PyObject *(*hi_to_py)(hashitem_T *);
1773
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001775DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001776{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001777 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001778 long_u todo = dict->dv_hashtab.ht_used;
1779 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001780 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001782 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001784 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001785 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1786 {
1787 if (!HASHITEM_EMPTY(hi))
1788 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789 if (!(newObj = hiconvert(hi)))
1790 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001791 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 return NULL;
1793 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001794 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 --todo;
1796 ++i;
1797 }
1798 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001799 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001800}
1801
Bram Moolenaara9922d62013-05-30 13:01:18 +02001802 static PyObject *
1803dict_key(hashitem_T *hi)
1804{
1805 return PyBytes_FromString((char *)(hi->hi_key));
1806}
1807
1808 static PyObject *
1809DictionaryListKeys(DictionaryObject *self)
1810{
1811 return DictionaryListObjects(self, dict_key);
1812}
1813
1814 static PyObject *
1815dict_val(hashitem_T *hi)
1816{
1817 dictitem_T *di;
1818
1819 di = dict_lookup(hi);
1820 return ConvertToPyObject(&di->di_tv);
1821}
1822
1823 static PyObject *
1824DictionaryListValues(DictionaryObject *self)
1825{
1826 return DictionaryListObjects(self, dict_val);
1827}
1828
1829 static PyObject *
1830dict_item(hashitem_T *hi)
1831{
1832 PyObject *keyObject;
1833 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001834 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001835
1836 if (!(keyObject = dict_key(hi)))
1837 return NULL;
1838
1839 if (!(valObject = dict_val(hi)))
1840 {
1841 Py_DECREF(keyObject);
1842 return NULL;
1843 }
1844
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001845 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001846
1847 Py_DECREF(keyObject);
1848 Py_DECREF(valObject);
1849
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001850 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001851}
1852
1853 static PyObject *
1854DictionaryListItems(DictionaryObject *self)
1855{
1856 return DictionaryListObjects(self, dict_item);
1857}
1858
1859 static PyObject *
1860DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1861{
1862 dict_T *dict = self->dict;
1863
1864 if (dict->dv_lock)
1865 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001866 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001867 return NULL;
1868 }
1869
1870 if (kwargs)
1871 {
1872 typval_T tv;
1873
1874 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1875 return NULL;
1876
1877 VimTryStart();
1878 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1879 clear_tv(&tv);
1880 if (VimTryEnd())
1881 return NULL;
1882 }
1883 else
1884 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001885 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001887 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888 return NULL;
1889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001890 if (PyObject_HasAttrString(obj, "keys"))
1891 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892 else
1893 {
1894 PyObject *iterator;
1895 PyObject *item;
1896
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001897 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001898 return NULL;
1899
1900 while ((item = PyIter_Next(iterator)))
1901 {
1902 PyObject *fast;
1903 PyObject *keyObject;
1904 PyObject *valObject;
1905 PyObject *todecref;
1906 char_u *key;
1907 dictitem_T *di;
1908
1909 if (!(fast = PySequence_Fast(item, "")))
1910 {
1911 Py_DECREF(iterator);
1912 Py_DECREF(item);
1913 return NULL;
1914 }
1915
1916 Py_DECREF(item);
1917
1918 if (PySequence_Fast_GET_SIZE(fast) != 2)
1919 {
1920 Py_DECREF(iterator);
1921 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001922 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001923 N_("expected sequence element of size 2, "
1924 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001925 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001926 return NULL;
1927 }
1928
1929 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1930
1931 if (!(key = StringToChars(keyObject, &todecref)))
1932 {
1933 Py_DECREF(iterator);
1934 Py_DECREF(fast);
1935 return NULL;
1936 }
1937
1938 di = dictitem_alloc(key);
1939
1940 Py_XDECREF(todecref);
1941
1942 if (di == NULL)
1943 {
1944 Py_DECREF(fast);
1945 Py_DECREF(iterator);
1946 PyErr_NoMemory();
1947 return NULL;
1948 }
1949 di->di_tv.v_lock = 0;
1950 di->di_tv.v_type = VAR_UNKNOWN;
1951
1952 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1953
1954 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1955 {
1956 Py_DECREF(iterator);
1957 Py_DECREF(fast);
1958 dictitem_free(di);
1959 return NULL;
1960 }
1961
1962 Py_DECREF(fast);
1963
1964 if (dict_add(dict, di) == FAIL)
1965 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001966 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001967 Py_DECREF(iterator);
1968 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001969 return NULL;
1970 }
1971 }
1972
1973 Py_DECREF(iterator);
1974
1975 /* Iterator may have finished due to an exception */
1976 if (PyErr_Occurred())
1977 return NULL;
1978 }
1979 }
1980 Py_INCREF(Py_None);
1981 return Py_None;
1982}
1983
1984 static PyObject *
1985DictionaryGet(DictionaryObject *self, PyObject *args)
1986{
1987 return _DictionaryItem(self, args,
1988 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1989}
1990
1991 static PyObject *
1992DictionaryPop(DictionaryObject *self, PyObject *args)
1993{
1994 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1995}
1996
1997 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001998DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001999{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002000 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002001 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002002 PyObject *valObject;
2003 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002004
Bram Moolenaarde71b562013-06-02 17:41:54 +02002005 if (self->dict->dv_hashtab.ht_used == 0)
2006 {
2007 PyErr_SetNone(PyExc_KeyError);
2008 return NULL;
2009 }
2010
2011 hi = self->dict->dv_hashtab.ht_array;
2012 while (HASHITEM_EMPTY(hi))
2013 ++hi;
2014
2015 di = dict_lookup(hi);
2016
2017 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018 return NULL;
2019
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002020 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002021 {
2022 Py_DECREF(valObject);
2023 return NULL;
2024 }
2025
2026 hash_remove(&self->dict->dv_hashtab, hi);
2027 dictitem_free(di);
2028
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002029 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002030}
2031
2032 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002033DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002034{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002035 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2036}
2037
2038static PySequenceMethods DictionaryAsSeq = {
2039 0, /* sq_length */
2040 0, /* sq_concat */
2041 0, /* sq_repeat */
2042 0, /* sq_item */
2043 0, /* sq_slice */
2044 0, /* sq_ass_item */
2045 0, /* sq_ass_slice */
2046 (objobjproc) DictionaryContains, /* sq_contains */
2047 0, /* sq_inplace_concat */
2048 0, /* sq_inplace_repeat */
2049};
2050
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002051static PyMappingMethods DictionaryAsMapping = {
2052 (lenfunc) DictionaryLength,
2053 (binaryfunc) DictionaryItem,
2054 (objobjargproc) DictionaryAssItem,
2055};
2056
Bram Moolenaardb913952012-06-29 12:54:53 +02002057static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002058 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2060 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2061 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2062 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2063 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002064 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002065 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002066 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2067 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002068};
2069
2070static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002071static PySequenceMethods ListAsSeq;
2072static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002073
2074typedef struct
2075{
2076 PyObject_HEAD
2077 list_T *list;
2078 pylinkedlist_T ref;
2079} ListObject;
2080
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002081#define NEW_LIST(list) ListNew(&ListType, list)
2082
Bram Moolenaardb913952012-06-29 12:54:53 +02002083 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002084ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002085{
2086 ListObject *self;
2087
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002088 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002089 if (self == NULL)
2090 return NULL;
2091 self->list = list;
2092 ++list->lv_refcount;
2093
2094 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2095
2096 return (PyObject *)(self);
2097}
2098
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002099 static list_T *
2100py_list_alloc()
2101{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002102 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002103
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002104 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002105 {
2106 PyErr_NoMemory();
2107 return NULL;
2108 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002109 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002110
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002111 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002112}
2113
2114 static int
2115list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2116{
2117 PyObject *iterator;
2118 PyObject *item;
2119 listitem_T *li;
2120
2121 if (!(iterator = PyObject_GetIter(obj)))
2122 return -1;
2123
2124 while ((item = PyIter_Next(iterator)))
2125 {
2126 if (!(li = listitem_alloc()))
2127 {
2128 PyErr_NoMemory();
2129 Py_DECREF(item);
2130 Py_DECREF(iterator);
2131 return -1;
2132 }
2133 li->li_tv.v_lock = 0;
2134 li->li_tv.v_type = VAR_UNKNOWN;
2135
2136 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2137 {
2138 Py_DECREF(item);
2139 Py_DECREF(iterator);
2140 listitem_free(li);
2141 return -1;
2142 }
2143
2144 Py_DECREF(item);
2145
2146 list_append(l, li);
2147 }
2148
2149 Py_DECREF(iterator);
2150
2151 /* Iterator may have finished due to an exception */
2152 if (PyErr_Occurred())
2153 return -1;
2154
2155 return 0;
2156}
2157
2158 static PyObject *
2159ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2160{
2161 list_T *list;
2162 PyObject *obj = NULL;
2163
2164 if (kwargs)
2165 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002166 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002167 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002168 return NULL;
2169 }
2170
2171 if (!PyArg_ParseTuple(args, "|O", &obj))
2172 return NULL;
2173
2174 if (!(list = py_list_alloc()))
2175 return NULL;
2176
2177 if (obj)
2178 {
2179 PyObject *lookup_dict;
2180
2181 if (!(lookup_dict = PyDict_New()))
2182 {
2183 list_unref(list);
2184 return NULL;
2185 }
2186
2187 if (list_py_concat(list, obj, lookup_dict) == -1)
2188 {
2189 Py_DECREF(lookup_dict);
2190 list_unref(list);
2191 return NULL;
2192 }
2193
2194 Py_DECREF(lookup_dict);
2195 }
2196
2197 return ListNew(subtype, list);
2198}
2199
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002200 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002202{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002203 pyll_remove(&self->ref, &lastlist);
2204 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002205
2206 DESTRUCTOR_FINISH(self);
2207}
2208
Bram Moolenaardb913952012-06-29 12:54:53 +02002209 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002210ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002211{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002212 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002213}
2214
2215 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002217{
2218 listitem_T *li;
2219
Bram Moolenaard6e39182013-05-21 18:30:34 +02002220 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002221 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002222 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002223 return NULL;
2224 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002225 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002226 if (li == NULL)
2227 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002228 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002229 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002230 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002231 return NULL;
2232 }
2233 return ConvertToPyObject(&li->li_tv);
2234}
2235
2236#define PROC_RANGE \
2237 if (last < 0) {\
2238 if (last < -size) \
2239 last = 0; \
2240 else \
2241 last += size; \
2242 } \
2243 if (first < 0) \
2244 first = 0; \
2245 if (first > size) \
2246 first = size; \
2247 if (last > size) \
2248 last = size;
2249
2250 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002251ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002252{
2253 PyInt i;
2254 PyInt size = ListLength(self);
2255 PyInt n;
2256 PyObject *list;
2257 int reversed = 0;
2258
2259 PROC_RANGE
2260 if (first >= last)
2261 first = last;
2262
2263 n = last-first;
2264 list = PyList_New(n);
2265 if (list == NULL)
2266 return NULL;
2267
2268 for (i = 0; i < n; ++i)
2269 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002270 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002271 if (item == NULL)
2272 {
2273 Py_DECREF(list);
2274 return NULL;
2275 }
2276
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002277 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002278 }
2279
2280 return list;
2281}
2282
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002283typedef struct
2284{
2285 listwatch_T lw;
2286 list_T *list;
2287} listiterinfo_T;
2288
2289 static void
2290ListIterDestruct(listiterinfo_T *lii)
2291{
2292 list_rem_watch(lii->list, &lii->lw);
2293 PyMem_Free(lii);
2294}
2295
2296 static PyObject *
2297ListIterNext(listiterinfo_T **lii)
2298{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002299 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002300
2301 if (!((*lii)->lw.lw_item))
2302 return NULL;
2303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002304 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002305 return NULL;
2306
2307 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2308
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002309 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002310}
2311
2312 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002313ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002314{
2315 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002316 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002317
2318 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2319 {
2320 PyErr_NoMemory();
2321 return NULL;
2322 }
2323
2324 list_add_watch(l, &lii->lw);
2325 lii->lw.lw_item = l->lv_first;
2326 lii->list = l;
2327
2328 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002329 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2330 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002331}
2332
Bram Moolenaardb913952012-06-29 12:54:53 +02002333 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002334ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002335{
2336 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002337 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002338 listitem_T *li;
2339 Py_ssize_t length = ListLength(self);
2340
2341 if (l->lv_lock)
2342 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002343 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002344 return -1;
2345 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002346 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002347 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002348 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002349 return -1;
2350 }
2351
2352 if (obj == NULL)
2353 {
2354 li = list_find(l, (long) index);
2355 list_remove(l, li, li);
2356 clear_tv(&li->li_tv);
2357 vim_free(li);
2358 return 0;
2359 }
2360
2361 if (ConvertFromPyObject(obj, &tv) == -1)
2362 return -1;
2363
2364 if (index == length)
2365 {
2366 if (list_append_tv(l, &tv) == FAIL)
2367 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002368 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002369 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002370 return -1;
2371 }
2372 }
2373 else
2374 {
2375 li = list_find(l, (long) index);
2376 clear_tv(&li->li_tv);
2377 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002378 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002379 }
2380 return 0;
2381}
2382
2383 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002384ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002385{
2386 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002387 PyObject *iterator;
2388 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 listitem_T *li;
2390 listitem_T *next;
2391 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002392 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002393 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002394
2395 if (l->lv_lock)
2396 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002397 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 return -1;
2399 }
2400
2401 PROC_RANGE
2402
2403 if (first == size)
2404 li = NULL;
2405 else
2406 {
2407 li = list_find(l, (long) first);
2408 if (li == NULL)
2409 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002410 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2411 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002412 return -1;
2413 }
2414 if (last > first)
2415 {
2416 i = last - first;
2417 while (i-- && li != NULL)
2418 {
2419 next = li->li_next;
2420 listitem_remove(l, li);
2421 li = next;
2422 }
2423 }
2424 }
2425
2426 if (obj == NULL)
2427 return 0;
2428
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002429 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002430 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002431
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002432 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002433 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002434 if (ConvertFromPyObject(item, &v) == -1)
2435 {
2436 Py_DECREF(iterator);
2437 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002438 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002439 }
2440 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002441 if (list_insert_tv(l, &v, li) == FAIL)
2442 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002443 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002444 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002445 return -1;
2446 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002447 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002448 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002449 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002450
2451 if (PyErr_Occurred())
2452 return -1;
2453
Bram Moolenaardb913952012-06-29 12:54:53 +02002454 return 0;
2455}
2456
2457 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002458ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002459{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002460 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002461 PyObject *lookup_dict;
2462
2463 if (l->lv_lock)
2464 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002465 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002466 return NULL;
2467 }
2468
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002469 if (!(lookup_dict = PyDict_New()))
2470 return NULL;
2471
Bram Moolenaardb913952012-06-29 12:54:53 +02002472 if (list_py_concat(l, obj, lookup_dict) == -1)
2473 {
2474 Py_DECREF(lookup_dict);
2475 return NULL;
2476 }
2477 Py_DECREF(lookup_dict);
2478
2479 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002480 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002481}
2482
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002483static char *ListAttrs[] = {
2484 "locked",
2485 NULL
2486};
2487
2488 static PyObject *
2489ListDir(PyObject *self)
2490{
2491 return ObjectDir(self, ListAttrs);
2492}
2493
Bram Moolenaar66b79852012-09-21 14:00:35 +02002494 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002495ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002496{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002497 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002498 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002499 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002500 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002501 return -1;
2502 }
2503
2504 if (strcmp(name, "locked") == 0)
2505 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002506 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002507 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002508 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002509 return -1;
2510 }
2511 else
2512 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002513 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002514 if (istrue == -1)
2515 return -1;
2516 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002517 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002518 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002519 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002520 }
2521 return 0;
2522 }
2523 else
2524 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002525 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002526 return -1;
2527 }
2528}
2529
Bram Moolenaardb913952012-06-29 12:54:53 +02002530static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002531 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2532 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2533 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002534};
2535
2536typedef struct
2537{
2538 PyObject_HEAD
2539 char_u *name;
2540} FunctionObject;
2541
2542static PyTypeObject FunctionType;
2543
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002544#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2545
Bram Moolenaardb913952012-06-29 12:54:53 +02002546 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002547FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002548{
2549 FunctionObject *self;
2550
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002551 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2552
Bram Moolenaardb913952012-06-29 12:54:53 +02002553 if (self == NULL)
2554 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002555
2556 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002557 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002558 if (!translated_function_exists(name))
2559 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002560 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002561 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002562 return NULL;
2563 }
2564 self->name = vim_strsave(name);
2565 func_ref(self->name);
2566 }
2567 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002568 if ((self->name = get_expanded_name(name,
2569 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2570 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002571 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002572 PyErr_FORMAT(PyExc_ValueError,
2573 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002574 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002575 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002576
2577 return (PyObject *)(self);
2578}
2579
2580 static PyObject *
2581FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2582{
2583 PyObject *self;
2584 char_u *name;
2585
2586 if (kwargs)
2587 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002588 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002589 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002590 return NULL;
2591 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002592
Bram Moolenaar389a1792013-06-23 13:00:44 +02002593 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002594 return NULL;
2595
2596 self = FunctionNew(subtype, name);
2597
Bram Moolenaar389a1792013-06-23 13:00:44 +02002598 PyMem_Free(name);
2599
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002600 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002601}
2602
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002603 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002604FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002605{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002606 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002607 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002608
2609 DESTRUCTOR_FINISH(self);
2610}
2611
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002612static char *FunctionAttrs[] = {
2613 "softspace",
2614 NULL
2615};
2616
2617 static PyObject *
2618FunctionDir(PyObject *self)
2619{
2620 return ObjectDir(self, FunctionAttrs);
2621}
2622
Bram Moolenaardb913952012-06-29 12:54:53 +02002623 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002624FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002625{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002626 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002627 typval_T args;
2628 typval_T selfdicttv;
2629 typval_T rettv;
2630 dict_T *selfdict = NULL;
2631 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002632 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002633 int error;
2634
2635 if (ConvertFromPyObject(argsObject, &args) == -1)
2636 return NULL;
2637
2638 if (kwargs != NULL)
2639 {
2640 selfdictObject = PyDict_GetItemString(kwargs, "self");
2641 if (selfdictObject != NULL)
2642 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002643 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002644 {
2645 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002646 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002647 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002648 selfdict = selfdicttv.vval.v_dict;
2649 }
2650 }
2651
Bram Moolenaar71700b82013-05-15 17:49:05 +02002652 Py_BEGIN_ALLOW_THREADS
2653 Python_Lock_Vim();
2654
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002655 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002656 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002657
2658 Python_Release_Vim();
2659 Py_END_ALLOW_THREADS
2660
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002661 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002662 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002663 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002664 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002665 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002666 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002667 }
2668 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002669 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002670
Bram Moolenaardb913952012-06-29 12:54:53 +02002671 clear_tv(&args);
2672 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002673 if (selfdict != NULL)
2674 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002675
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002676 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002677}
2678
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002679 static PyObject *
2680FunctionRepr(FunctionObject *self)
2681{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002682#ifdef Py_TRACE_REFS
2683 /* For unknown reason self->name may be NULL after calling
2684 * Finalize */
2685 return PyString_FromFormat("<vim.Function '%s'>",
2686 (self->name == NULL ? "<NULL>" : (char *) self->name));
2687#else
2688 return PyString_FromFormat("<vim.Function '%s'>", (char *) self->name);
2689#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002690}
2691
Bram Moolenaardb913952012-06-29 12:54:53 +02002692static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002693 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2694 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002695};
2696
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002697/*
2698 * Options object
2699 */
2700
2701static PyTypeObject OptionsType;
2702
2703typedef int (*checkfun)(void *);
2704
2705typedef struct
2706{
2707 PyObject_HEAD
2708 int opt_type;
2709 void *from;
2710 checkfun Check;
2711 PyObject *fromObj;
2712} OptionsObject;
2713
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002714 static int
2715dummy_check(void *arg UNUSED)
2716{
2717 return 0;
2718}
2719
2720 static PyObject *
2721OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2722{
2723 OptionsObject *self;
2724
Bram Moolenaar774267b2013-05-21 20:51:59 +02002725 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002726 if (self == NULL)
2727 return NULL;
2728
2729 self->opt_type = opt_type;
2730 self->from = from;
2731 self->Check = Check;
2732 self->fromObj = fromObj;
2733 if (fromObj)
2734 Py_INCREF(fromObj);
2735
2736 return (PyObject *)(self);
2737}
2738
2739 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002740OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002741{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002742 PyObject_GC_UnTrack((void *)(self));
2743 Py_XDECREF(self->fromObj);
2744 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002745}
2746
2747 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002748OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002749{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002750 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002751 return 0;
2752}
2753
2754 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002755OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002756{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002757 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002758 return 0;
2759}
2760
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002761 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002762OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002763{
2764 char_u *key;
2765 int flags;
2766 long numval;
2767 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002768 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002769
Bram Moolenaard6e39182013-05-21 18:30:34 +02002770 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002771 return NULL;
2772
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002773 if (!(key = StringToChars(keyObject, &todecref)))
2774 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002775
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002776 if (*key == NUL)
2777 {
2778 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002779 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002780 return NULL;
2781 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002782
2783 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002784 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002785
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002786 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002787
2788 if (flags == 0)
2789 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002790 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002791 return NULL;
2792 }
2793
2794 if (flags & SOPT_UNSET)
2795 {
2796 Py_INCREF(Py_None);
2797 return Py_None;
2798 }
2799 else if (flags & SOPT_BOOL)
2800 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002801 PyObject *ret;
2802 ret = numval ? Py_True : Py_False;
2803 Py_INCREF(ret);
2804 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002805 }
2806 else if (flags & SOPT_NUM)
2807 return PyInt_FromLong(numval);
2808 else if (flags & SOPT_STRING)
2809 {
2810 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002811 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002812 PyObject *ret = PyBytes_FromString((char *) stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002813 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002814 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002815 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002816 else
2817 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002818 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002819 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002820 return NULL;
2821 }
2822 }
2823 else
2824 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002825 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002826 return NULL;
2827 }
2828}
2829
2830 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002831set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002832{
2833 char_u *errmsg;
2834
2835 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2836 {
2837 if (VimTryEnd())
2838 return FAIL;
2839 PyErr_SetVim((char *)errmsg);
2840 return FAIL;
2841 }
2842 return OK;
2843}
2844
2845 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002846set_option_value_for(
2847 char_u *key,
2848 int numval,
2849 char_u *stringval,
2850 int opt_flags,
2851 int opt_type,
2852 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002853{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002854 win_T *save_curwin = NULL;
2855 tabpage_T *save_curtab = NULL;
2856 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002857 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002858
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002859 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002860 switch (opt_type)
2861 {
2862 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002863 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002864 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002865 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002866 if (VimTryEnd())
2867 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002868 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002869 return -1;
2870 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2872 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002873 break;
2874 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002875 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002876 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002877 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002878 break;
2879 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002880 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002881 break;
2882 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002883 if (set_ret == FAIL)
2884 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002885 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002886}
2887
2888 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002889OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002890{
2891 char_u *key;
2892 int flags;
2893 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002894 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002895 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002896
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002898 return -1;
2899
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002900 if (!(key = StringToChars(keyObject, &todecref)))
2901 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002902
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002903 if (*key == NUL)
2904 {
2905 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002906 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002907 return -1;
2908 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002909
2910 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002911 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002912
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002913 if (flags == 0)
2914 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002915 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002916 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002917 return -1;
2918 }
2919
2920 if (valObject == NULL)
2921 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002922 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002924 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002925 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002926 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002927 return -1;
2928 }
2929 else if (!(flags & SOPT_GLOBAL))
2930 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002931 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002932 N_("unable to unset option %s "
2933 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002934 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002935 return -1;
2936 }
2937 else
2938 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002939 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002940 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002941 return 0;
2942 }
2943 }
2944
Bram Moolenaard6e39182013-05-21 18:30:34 +02002945 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002946
2947 if (flags & SOPT_BOOL)
2948 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002949 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002950
Bram Moolenaarb983f752013-05-15 16:11:50 +02002951 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002952 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002953 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002954 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002955 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002956 }
2957 else if (flags & SOPT_NUM)
2958 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002959 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002960
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002961 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002962 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002963 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002964 return -1;
2965 }
2966
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002967 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002968 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002969 }
2970 else
2971 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002972 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002973 PyObject *todecref;
2974
2975 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002976 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002977 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002978 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002979 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002980 }
2981
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002982 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002983
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002984 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002985}
2986
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002987static PyMappingMethods OptionsAsMapping = {
2988 (lenfunc) NULL,
2989 (binaryfunc) OptionsItem,
2990 (objobjargproc) OptionsAssItem,
2991};
2992
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002993/* Tabpage object
2994 */
2995
2996typedef struct
2997{
2998 PyObject_HEAD
2999 tabpage_T *tab;
3000} TabPageObject;
3001
3002static PyObject *WinListNew(TabPageObject *tabObject);
3003
3004static PyTypeObject TabPageType;
3005
3006 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003007CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003008{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003009 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003010 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003011 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003012 return -1;
3013 }
3014
3015 return 0;
3016}
3017
3018 static PyObject *
3019TabPageNew(tabpage_T *tab)
3020{
3021 TabPageObject *self;
3022
3023 if (TAB_PYTHON_REF(tab))
3024 {
3025 self = TAB_PYTHON_REF(tab);
3026 Py_INCREF(self);
3027 }
3028 else
3029 {
3030 self = PyObject_NEW(TabPageObject, &TabPageType);
3031 if (self == NULL)
3032 return NULL;
3033 self->tab = tab;
3034 TAB_PYTHON_REF(tab) = self;
3035 }
3036
3037 return (PyObject *)(self);
3038}
3039
3040 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003041TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003042{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003043 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3044 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003045
3046 DESTRUCTOR_FINISH(self);
3047}
3048
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003049static char *TabPageAttrs[] = {
3050 "windows", "number", "vars", "window", "valid",
3051 NULL
3052};
3053
3054 static PyObject *
3055TabPageDir(PyObject *self)
3056{
3057 return ObjectDir(self, TabPageAttrs);
3058}
3059
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003060 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003061TabPageAttrValid(TabPageObject *self, char *name)
3062{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003063 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003064
3065 if (strcmp(name, "valid") != 0)
3066 return NULL;
3067
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003068 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3069 Py_INCREF(ret);
3070 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003071}
3072
3073 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003074TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003075{
3076 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003077 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003078 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003079 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003080 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003081 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003082 else if (strcmp(name, "window") == 0)
3083 {
3084 /* For current tab window.c does not bother to set or update tp_curwin
3085 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003086 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003087 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003088 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003089 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003090 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003091 else if (strcmp(name, "__members__") == 0)
3092 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003093 return NULL;
3094}
3095
3096 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003097TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003098{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003100 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003101 else
3102 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003103 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003104
3105 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003106 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3107 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003108 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003109 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003110 }
3111}
3112
3113static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003114 /* name, function, calling, documentation */
3115 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3116 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003117};
3118
3119/*
3120 * Window list object
3121 */
3122
3123static PyTypeObject TabListType;
3124static PySequenceMethods TabListAsSeq;
3125
3126typedef struct
3127{
3128 PyObject_HEAD
3129} TabListObject;
3130
3131 static PyInt
3132TabListLength(PyObject *self UNUSED)
3133{
3134 tabpage_T *tp = first_tabpage;
3135 PyInt n = 0;
3136
3137 while (tp != NULL)
3138 {
3139 ++n;
3140 tp = tp->tp_next;
3141 }
3142
3143 return n;
3144}
3145
3146 static PyObject *
3147TabListItem(PyObject *self UNUSED, PyInt n)
3148{
3149 tabpage_T *tp;
3150
3151 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3152 if (n == 0)
3153 return TabPageNew(tp);
3154
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003155 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003156 return NULL;
3157}
3158
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003159/*
3160 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003161 */
3162
3163typedef struct
3164{
3165 PyObject_HEAD
3166 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003167 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003168} WindowObject;
3169
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003170static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003171
3172 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003173CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003175 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003176 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003177 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003178 return -1;
3179 }
3180
3181 return 0;
3182}
3183
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003184 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003185WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003186{
3187 /* We need to handle deletion of windows underneath us.
3188 * If we add a "w_python*_ref" field to the win_T structure,
3189 * then we can get at it in win_free() in vim. We then
3190 * need to create only ONE Python object per window - if
3191 * we try to create a second, just INCREF the existing one
3192 * and return it. The (single) Python object referring to
3193 * the window is stored in "w_python*_ref".
3194 * On a win_free() we set the Python object's win_T* field
3195 * to an invalid value. We trap all uses of a window
3196 * object, and reject them if the win_T* field is invalid.
3197 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003198 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003199 * w_python_ref and w_python3_ref fields respectively.
3200 */
3201
3202 WindowObject *self;
3203
3204 if (WIN_PYTHON_REF(win))
3205 {
3206 self = WIN_PYTHON_REF(win);
3207 Py_INCREF(self);
3208 }
3209 else
3210 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003211 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003212 if (self == NULL)
3213 return NULL;
3214 self->win = win;
3215 WIN_PYTHON_REF(win) = self;
3216 }
3217
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003218 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3219
Bram Moolenaar971db462013-05-12 18:44:48 +02003220 return (PyObject *)(self);
3221}
3222
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003223 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003225{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003226 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003227 if (self->win && self->win != INVALID_WINDOW_VALUE)
3228 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003229 Py_XDECREF(((PyObject *)(self->tabObject)));
3230 PyObject_GC_Del((void *)(self));
3231}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003232
Bram Moolenaar774267b2013-05-21 20:51:59 +02003233 static int
3234WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3235{
3236 Py_VISIT(((PyObject *)(self->tabObject)));
3237 return 0;
3238}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003239
Bram Moolenaar774267b2013-05-21 20:51:59 +02003240 static int
3241WindowClear(WindowObject *self)
3242{
3243 Py_CLEAR(self->tabObject);
3244 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003245}
3246
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003247 static win_T *
3248get_firstwin(TabPageObject *tabObject)
3249{
3250 if (tabObject)
3251 {
3252 if (CheckTabPage(tabObject))
3253 return NULL;
3254 /* For current tab window.c does not bother to set or update tp_firstwin
3255 */
3256 else if (tabObject->tab == curtab)
3257 return firstwin;
3258 else
3259 return tabObject->tab->tp_firstwin;
3260 }
3261 else
3262 return firstwin;
3263}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003264static char *WindowAttrs[] = {
3265 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3266 "tabpage", "valid",
3267 NULL
3268};
3269
3270 static PyObject *
3271WindowDir(PyObject *self)
3272{
3273 return ObjectDir(self, WindowAttrs);
3274}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003275
Bram Moolenaar971db462013-05-12 18:44:48 +02003276 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003277WindowAttrValid(WindowObject *self, char *name)
3278{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003279 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003280
3281 if (strcmp(name, "valid") != 0)
3282 return NULL;
3283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003284 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3285 Py_INCREF(ret);
3286 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003287}
3288
3289 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003290WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003291{
3292 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003293 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003294 else if (strcmp(name, "cursor") == 0)
3295 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003296 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003297
3298 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3299 }
3300 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003301 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003302#ifdef FEAT_WINDOWS
3303 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003304 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003305#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003306#ifdef FEAT_VERTSPLIT
3307 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003308 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003309 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003310 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003311#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003312 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003313 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003314 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003315 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3316 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003317 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003318 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003319 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003320 return NULL;
3321 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003322 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003323 }
3324 else if (strcmp(name, "tabpage") == 0)
3325 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003326 Py_INCREF(self->tabObject);
3327 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003328 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003329 else if (strcmp(name, "__members__") == 0)
3330 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003331 else
3332 return NULL;
3333}
3334
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003335 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003336WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003337{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003339 return -1;
3340
3341 if (strcmp(name, "buffer") == 0)
3342 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003343 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003344 return -1;
3345 }
3346 else if (strcmp(name, "cursor") == 0)
3347 {
3348 long lnum;
3349 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003350
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003351 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352 return -1;
3353
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003355 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003356 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003357 return -1;
3358 }
3359
3360 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003361 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003362 return -1;
3363
Bram Moolenaard6e39182013-05-21 18:30:34 +02003364 self->win->w_cursor.lnum = lnum;
3365 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003366#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003368#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003369 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003371
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003372 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003373 return 0;
3374 }
3375 else if (strcmp(name, "height") == 0)
3376 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003377 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003378 win_T *savewin;
3379
Bram Moolenaardee2e312013-06-23 16:35:47 +02003380 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003381 return -1;
3382
3383#ifdef FEAT_GUI
3384 need_mouse_correct = TRUE;
3385#endif
3386 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003387 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003388
3389 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003390 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003391 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003392 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003393 return -1;
3394
3395 return 0;
3396 }
3397#ifdef FEAT_VERTSPLIT
3398 else if (strcmp(name, "width") == 0)
3399 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003400 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003401 win_T *savewin;
3402
Bram Moolenaardee2e312013-06-23 16:35:47 +02003403 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404 return -1;
3405
3406#ifdef FEAT_GUI
3407 need_mouse_correct = TRUE;
3408#endif
3409 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003410 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003411
3412 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003413 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003414 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003415 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003416 return -1;
3417
3418 return 0;
3419 }
3420#endif
3421 else
3422 {
3423 PyErr_SetString(PyExc_AttributeError, name);
3424 return -1;
3425 }
3426}
3427
3428 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003429WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003430{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003431 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003432 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003433 else
3434 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003435 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003436
Bram Moolenaar6d216452013-05-12 19:00:41 +02003437 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003438 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003439 (self));
3440 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003441 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003442 }
3443}
3444
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003445static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003446 /* name, function, calling, documentation */
3447 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3448 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003449};
3450
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003451/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003452 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003453 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003454
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003455static PyTypeObject WinListType;
3456static PySequenceMethods WinListAsSeq;
3457
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003458typedef struct
3459{
3460 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003461 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003462} WinListObject;
3463
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003464 static PyObject *
3465WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003466{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003467 WinListObject *self;
3468
3469 self = PyObject_NEW(WinListObject, &WinListType);
3470 self->tabObject = tabObject;
3471 Py_INCREF(tabObject);
3472
3473 return (PyObject *)(self);
3474}
3475
3476 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003477WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003478{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003479 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003480
3481 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003482 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003483 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003484 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003485
3486 DESTRUCTOR_FINISH(self);
3487}
3488
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003489 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003490WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003491{
3492 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003493 PyInt n = 0;
3494
Bram Moolenaard6e39182013-05-21 18:30:34 +02003495 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003496 return -1;
3497
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003498 while (w != NULL)
3499 {
3500 ++n;
3501 w = W_NEXT(w);
3502 }
3503
3504 return n;
3505}
3506
3507 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003508WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509{
3510 win_T *w;
3511
Bram Moolenaard6e39182013-05-21 18:30:34 +02003512 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003513 return NULL;
3514
3515 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003516 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003517 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003518
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003519 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003520 return NULL;
3521}
3522
3523/* Convert a Python string into a Vim line.
3524 *
3525 * The result is in allocated memory. All internal nulls are replaced by
3526 * newline characters. It is an error for the string to contain newline
3527 * characters.
3528 *
3529 * On errors, the Python exception data is set, and NULL is returned.
3530 */
3531 static char *
3532StringToLine(PyObject *obj)
3533{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003534 char *str;
3535 char *save;
3536 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003537 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003538 PyInt i;
3539 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003540
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003541 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003542 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003543 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3544 || str == NULL)
3545 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003546 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003547 else if (PyUnicode_Check(obj))
3548 {
3549 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3550 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003551
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003552 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3553 || str == NULL)
3554 {
3555 Py_DECREF(bytes);
3556 return NULL;
3557 }
3558 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559
3560 /*
3561 * Error checking: String must not contain newlines, as we
3562 * are replacing a single line, and we must replace it with
3563 * a single line.
3564 * A trailing newline is removed, so that append(f.readlines()) works.
3565 */
3566 p = memchr(str, '\n', len);
3567 if (p != NULL)
3568 {
3569 if (p == str + len - 1)
3570 --len;
3571 else
3572 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003573 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003574 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003575 return NULL;
3576 }
3577 }
3578
3579 /* Create a copy of the string, with internal nulls replaced by
3580 * newline characters, as is the vim convention.
3581 */
3582 save = (char *)alloc((unsigned)(len+1));
3583 if (save == NULL)
3584 {
3585 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003586 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003587 return NULL;
3588 }
3589
3590 for (i = 0; i < len; ++i)
3591 {
3592 if (str[i] == '\0')
3593 save[i] = '\n';
3594 else
3595 save[i] = str[i];
3596 }
3597
3598 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003599 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003600
3601 return save;
3602}
3603
3604/* Get a line from the specified buffer. The line number is
3605 * in Vim format (1-based). The line is returned as a Python
3606 * string object.
3607 */
3608 static PyObject *
3609GetBufferLine(buf_T *buf, PyInt n)
3610{
3611 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3612}
3613
3614
3615/* Get a list of lines from the specified buffer. The line numbers
3616 * are in Vim format (1-based). The range is from lo up to, but not
3617 * including, hi. The list is returned as a Python list of string objects.
3618 */
3619 static PyObject *
3620GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3621{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003622 PyInt i;
3623 PyInt n = hi - lo;
3624 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003625
3626 if (list == NULL)
3627 return NULL;
3628
3629 for (i = 0; i < n; ++i)
3630 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 PyObject *string = LineToString(
3632 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003633
3634 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003635 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003636 {
3637 Py_DECREF(list);
3638 return NULL;
3639 }
3640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003641 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003642 }
3643
3644 /* The ownership of the Python list is passed to the caller (ie,
3645 * the caller should Py_DECREF() the object when it is finished
3646 * with it).
3647 */
3648
3649 return list;
3650}
3651
3652/*
3653 * Check if deleting lines made the cursor position invalid.
3654 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3655 * deleted).
3656 */
3657 static void
3658py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3659{
3660 if (curwin->w_cursor.lnum >= lo)
3661 {
3662 /* Adjust the cursor position if it's in/after the changed
3663 * lines. */
3664 if (curwin->w_cursor.lnum >= hi)
3665 {
3666 curwin->w_cursor.lnum += extra;
3667 check_cursor_col();
3668 }
3669 else if (extra < 0)
3670 {
3671 curwin->w_cursor.lnum = lo;
3672 check_cursor();
3673 }
3674 else
3675 check_cursor_col();
3676 changed_cline_bef_curs();
3677 }
3678 invalidate_botline();
3679}
3680
Bram Moolenaar19e60942011-06-19 00:27:51 +02003681/*
3682 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003683 * in Vim format (1-based). The replacement line is given as
3684 * a Python string object. The object is checked for validity
3685 * and correct format. Errors are returned as a value of FAIL.
3686 * The return value is OK on success.
3687 * If OK is returned and len_change is not NULL, *len_change
3688 * is set to the change in the buffer length.
3689 */
3690 static int
3691SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3692{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003693 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694 * There are three cases:
3695 * 1. NULL, or None - this is a deletion.
3696 * 2. A string - this is a replacement.
3697 * 3. Anything else - this is an error.
3698 */
3699 if (line == Py_None || line == NULL)
3700 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003701 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003702
3703 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003704 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003705
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003706 VimTryStart();
3707
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003709 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003711 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003712 else
3713 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003714 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3716 deleted_lines_mark((linenr_T)n, 1L);
3717 }
3718
Bram Moolenaar105bc352013-05-17 16:03:57 +02003719 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003720
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003721 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003722 return FAIL;
3723
3724 if (len_change)
3725 *len_change = -1;
3726
3727 return OK;
3728 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003729 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003731 char *save = StringToLine(line);
3732 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003733
3734 if (save == NULL)
3735 return FAIL;
3736
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003737 VimTryStart();
3738
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739 /* We do not need to free "save" if ml_replace() consumes it. */
3740 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003741 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742
3743 if (u_savesub((linenr_T)n) == FAIL)
3744 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003745 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003746 vim_free(save);
3747 }
3748 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3749 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003750 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003751 vim_free(save);
3752 }
3753 else
3754 changed_bytes((linenr_T)n, 0);
3755
Bram Moolenaar105bc352013-05-17 16:03:57 +02003756 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003757
3758 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003759 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003760 check_cursor_col();
3761
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003762 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003763 return FAIL;
3764
3765 if (len_change)
3766 *len_change = 0;
3767
3768 return OK;
3769 }
3770 else
3771 {
3772 PyErr_BadArgument();
3773 return FAIL;
3774 }
3775}
3776
Bram Moolenaar19e60942011-06-19 00:27:51 +02003777/* Replace a range of lines in the specified buffer. The line numbers are in
3778 * Vim format (1-based). The range is from lo up to, but not including, hi.
3779 * The replacement lines are given as a Python list of string objects. The
3780 * list is checked for validity and correct format. Errors are returned as a
3781 * value of FAIL. The return value is OK on success.
3782 * If OK is returned and len_change is not NULL, *len_change
3783 * is set to the change in the buffer length.
3784 */
3785 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003786SetBufferLineList(
3787 buf_T *buf,
3788 PyInt lo,
3789 PyInt hi,
3790 PyObject *list,
3791 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003792{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003793 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003794 * There are three cases:
3795 * 1. NULL, or None - this is a deletion.
3796 * 2. A list - this is a replacement.
3797 * 3. Anything else - this is an error.
3798 */
3799 if (list == Py_None || list == NULL)
3800 {
3801 PyInt i;
3802 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003803 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003804
3805 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003806 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003807 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003808
3809 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003810 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003811 else
3812 {
3813 for (i = 0; i < n; ++i)
3814 {
3815 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3816 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003817 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003818 break;
3819 }
3820 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003821 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003822 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3823 deleted_lines_mark((linenr_T)lo, (long)i);
3824 }
3825
Bram Moolenaar105bc352013-05-17 16:03:57 +02003826 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003827
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003828 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003829 return FAIL;
3830
3831 if (len_change)
3832 *len_change = -n;
3833
3834 return OK;
3835 }
3836 else if (PyList_Check(list))
3837 {
3838 PyInt i;
3839 PyInt new_len = PyList_Size(list);
3840 PyInt old_len = hi - lo;
3841 PyInt extra = 0; /* lines added to text, can be negative */
3842 char **array;
3843 buf_T *savebuf;
3844
3845 if (new_len == 0) /* avoid allocating zero bytes */
3846 array = NULL;
3847 else
3848 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003849 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003850 if (array == NULL)
3851 {
3852 PyErr_NoMemory();
3853 return FAIL;
3854 }
3855 }
3856
3857 for (i = 0; i < new_len; ++i)
3858 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003859 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003860
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003861 if (!(line = PyList_GetItem(list, i)) ||
3862 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003863 {
3864 while (i)
3865 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003866 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003867 return FAIL;
3868 }
3869 }
3870
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003871 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003872 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003873
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003874 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003875 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003876
3877 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003878 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003879
3880 /* If the size of the range is reducing (ie, new_len < old_len) we
3881 * need to delete some old_len. We do this at the start, by
3882 * repeatedly deleting line "lo".
3883 */
3884 if (!PyErr_Occurred())
3885 {
3886 for (i = 0; i < old_len - new_len; ++i)
3887 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3888 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003889 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003890 break;
3891 }
3892 extra -= i;
3893 }
3894
3895 /* For as long as possible, replace the existing old_len with the
3896 * new old_len. This is a more efficient operation, as it requires
3897 * less memory allocation and freeing.
3898 */
3899 if (!PyErr_Occurred())
3900 {
3901 for (i = 0; i < old_len && i < new_len; ++i)
3902 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3903 == FAIL)
3904 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003905 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003906 break;
3907 }
3908 }
3909 else
3910 i = 0;
3911
3912 /* Now we may need to insert the remaining new old_len. If we do, we
3913 * must free the strings as we finish with them (we can't pass the
3914 * responsibility to vim in this case).
3915 */
3916 if (!PyErr_Occurred())
3917 {
3918 while (i < new_len)
3919 {
3920 if (ml_append((linenr_T)(lo + i - 1),
3921 (char_u *)array[i], 0, FALSE) == FAIL)
3922 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003923 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003924 break;
3925 }
3926 vim_free(array[i]);
3927 ++i;
3928 ++extra;
3929 }
3930 }
3931
3932 /* Free any left-over old_len, as a result of an error */
3933 while (i < new_len)
3934 {
3935 vim_free(array[i]);
3936 ++i;
3937 }
3938
3939 /* Free the array of old_len. All of its contents have now
3940 * been dealt with (either freed, or the responsibility passed
3941 * to vim.
3942 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003943 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003944
3945 /* Adjust marks. Invalidate any which lie in the
3946 * changed range, and move any in the remainder of the buffer.
3947 */
3948 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3949 (long)MAXLNUM, (long)extra);
3950 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3951
Bram Moolenaar105bc352013-05-17 16:03:57 +02003952 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003953 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3954
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003955 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003956 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003957
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003958 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003959 return FAIL;
3960
3961 if (len_change)
3962 *len_change = new_len - old_len;
3963
3964 return OK;
3965 }
3966 else
3967 {
3968 PyErr_BadArgument();
3969 return FAIL;
3970 }
3971}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003973/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003974 * The line number is in Vim format (1-based). The lines to be inserted are
3975 * given as a Python list of string objects or as a single string. The lines
3976 * to be added are checked for validity and correct format. Errors are
3977 * returned as a value of FAIL. The return value is OK on success.
3978 * If OK is returned and len_change is not NULL, *len_change
3979 * is set to the change in the buffer length.
3980 */
3981 static int
3982InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3983{
3984 /* First of all, we check the type of the supplied Python object.
3985 * It must be a string or a list, or the call is in error.
3986 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003987 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988 {
3989 char *str = StringToLine(lines);
3990 buf_T *savebuf;
3991
3992 if (str == NULL)
3993 return FAIL;
3994
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003995 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003996 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003997 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003998
3999 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004000 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004002 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003 else
4004 appended_lines_mark((linenr_T)n, 1L);
4005
4006 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004007 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 update_screen(VALID);
4009
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004010 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011 return FAIL;
4012
4013 if (len_change)
4014 *len_change = 1;
4015
4016 return OK;
4017 }
4018 else if (PyList_Check(lines))
4019 {
4020 PyInt i;
4021 PyInt size = PyList_Size(lines);
4022 char **array;
4023 buf_T *savebuf;
4024
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004025 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 if (array == NULL)
4027 {
4028 PyErr_NoMemory();
4029 return FAIL;
4030 }
4031
4032 for (i = 0; i < size; ++i)
4033 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004034 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004036 if (!(line = PyList_GetItem(lines, i)) ||
4037 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 {
4039 while (i)
4040 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004041 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042 return FAIL;
4043 }
4044 }
4045
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004047 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004048 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049
4050 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004051 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 else
4053 {
4054 for (i = 0; i < size; ++i)
4055 {
4056 if (ml_append((linenr_T)(n + i),
4057 (char_u *)array[i], 0, FALSE) == FAIL)
4058 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004059 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004060
4061 /* Free the rest of the lines */
4062 while (i < size)
4063 vim_free(array[i++]);
4064
4065 break;
4066 }
4067 vim_free(array[i]);
4068 }
4069 if (i > 0)
4070 appended_lines_mark((linenr_T)n, (long)i);
4071 }
4072
4073 /* Free the array of lines. All of its contents have now
4074 * been freed.
4075 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004076 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077
Bram Moolenaar105bc352013-05-17 16:03:57 +02004078 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 update_screen(VALID);
4080
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004081 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004082 return FAIL;
4083
4084 if (len_change)
4085 *len_change = size;
4086
4087 return OK;
4088 }
4089 else
4090 {
4091 PyErr_BadArgument();
4092 return FAIL;
4093 }
4094}
4095
4096/*
4097 * Common routines for buffers and line ranges
4098 * -------------------------------------------
4099 */
4100
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004101typedef struct
4102{
4103 PyObject_HEAD
4104 buf_T *buf;
4105} BufferObject;
4106
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004108CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004110 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004112 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 return -1;
4114 }
4115
4116 return 0;
4117}
4118
4119 static PyObject *
4120RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4121{
4122 if (CheckBuffer(self))
4123 return NULL;
4124
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004125 if (end == -1)
4126 end = self->buf->b_ml.ml_line_count;
4127
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004128 if (n < 0)
4129 n += end - start + 1;
4130
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 if (n < 0 || n > end - start)
4132 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004133 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134 return NULL;
4135 }
4136
4137 return GetBufferLine(self->buf, n+start);
4138}
4139
4140 static PyObject *
4141RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4142{
4143 PyInt size;
4144
4145 if (CheckBuffer(self))
4146 return NULL;
4147
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004148 if (end == -1)
4149 end = self->buf->b_ml.ml_line_count;
4150
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004151 size = end - start + 1;
4152
4153 if (lo < 0)
4154 lo = 0;
4155 else if (lo > size)
4156 lo = size;
4157 if (hi < 0)
4158 hi = 0;
4159 if (hi < lo)
4160 hi = lo;
4161 else if (hi > size)
4162 hi = size;
4163
4164 return GetBufferLineList(self->buf, lo+start, hi+start);
4165}
4166
4167 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004168RBAsItem(
4169 BufferObject *self,
4170 PyInt n,
4171 PyObject *valObject,
4172 PyInt start,
4173 PyInt end,
4174 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175{
4176 PyInt len_change;
4177
4178 if (CheckBuffer(self))
4179 return -1;
4180
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004181 if (end == -1)
4182 end = self->buf->b_ml.ml_line_count;
4183
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004184 if (n < 0)
4185 n += end - start + 1;
4186
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004187 if (n < 0 || n > end - start)
4188 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004189 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 return -1;
4191 }
4192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004193 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194 return -1;
4195
4196 if (new_end)
4197 *new_end = end + len_change;
4198
4199 return 0;
4200}
4201
Bram Moolenaar19e60942011-06-19 00:27:51 +02004202 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004203RBAsSlice(
4204 BufferObject *self,
4205 PyInt lo,
4206 PyInt hi,
4207 PyObject *valObject,
4208 PyInt start,
4209 PyInt end,
4210 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004211{
4212 PyInt size;
4213 PyInt len_change;
4214
4215 /* Self must be a valid buffer */
4216 if (CheckBuffer(self))
4217 return -1;
4218
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004219 if (end == -1)
4220 end = self->buf->b_ml.ml_line_count;
4221
Bram Moolenaar19e60942011-06-19 00:27:51 +02004222 /* Sort out the slice range */
4223 size = end - start + 1;
4224
4225 if (lo < 0)
4226 lo = 0;
4227 else if (lo > size)
4228 lo = size;
4229 if (hi < 0)
4230 hi = 0;
4231 if (hi < lo)
4232 hi = lo;
4233 else if (hi > size)
4234 hi = size;
4235
4236 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004237 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004238 return -1;
4239
4240 if (new_end)
4241 *new_end = end + len_change;
4242
4243 return 0;
4244}
4245
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004246
4247 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004248RBAppend(
4249 BufferObject *self,
4250 PyObject *args,
4251 PyInt start,
4252 PyInt end,
4253 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004254{
4255 PyObject *lines;
4256 PyInt len_change;
4257 PyInt max;
4258 PyInt n;
4259
4260 if (CheckBuffer(self))
4261 return NULL;
4262
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004263 if (end == -1)
4264 end = self->buf->b_ml.ml_line_count;
4265
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266 max = n = end - start + 1;
4267
4268 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4269 return NULL;
4270
4271 if (n < 0 || n > max)
4272 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004273 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004274 return NULL;
4275 }
4276
4277 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4278 return NULL;
4279
4280 if (new_end)
4281 *new_end = end + len_change;
4282
4283 Py_INCREF(Py_None);
4284 return Py_None;
4285}
4286
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004287/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004288 */
4289
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004290static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004291static PySequenceMethods RangeAsSeq;
4292static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004293
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294typedef struct
4295{
4296 PyObject_HEAD
4297 BufferObject *buf;
4298 PyInt start;
4299 PyInt end;
4300} RangeObject;
4301
4302 static PyObject *
4303RangeNew(buf_T *buf, PyInt start, PyInt end)
4304{
4305 BufferObject *bufr;
4306 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004307 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004308 if (self == NULL)
4309 return NULL;
4310
4311 bufr = (BufferObject *)BufferNew(buf);
4312 if (bufr == NULL)
4313 {
4314 Py_DECREF(self);
4315 return NULL;
4316 }
4317 Py_INCREF(bufr);
4318
4319 self->buf = bufr;
4320 self->start = start;
4321 self->end = end;
4322
4323 return (PyObject *)(self);
4324}
4325
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004326 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004327RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004328{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004329 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004330 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004331 PyObject_GC_Del((void *)(self));
4332}
4333
4334 static int
4335RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4336{
4337 Py_VISIT(((PyObject *)(self->buf)));
4338 return 0;
4339}
4340
4341 static int
4342RangeClear(RangeObject *self)
4343{
4344 Py_CLEAR(self->buf);
4345 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004346}
4347
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004348 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004349RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004350{
4351 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004352 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004353 return -1; /* ??? */
4354
Bram Moolenaard6e39182013-05-21 18:30:34 +02004355 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004356}
4357
4358 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004359RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004361 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004362}
4363
4364 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004365RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004367 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004368}
4369
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004370static char *RangeAttrs[] = {
4371 "start", "end",
4372 NULL
4373};
4374
4375 static PyObject *
4376RangeDir(PyObject *self)
4377{
4378 return ObjectDir(self, RangeAttrs);
4379}
4380
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004381 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004382RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004383{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004384 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004385}
4386
4387 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004388RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004389{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004390 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004391 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4392 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004393 else
4394 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004395 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004396
4397 if (name == NULL)
4398 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004399
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004400 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004401 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004402 }
4403}
4404
4405static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004406 /* name, function, calling, documentation */
4407 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004408 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4409 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004410};
4411
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004412static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004413static PySequenceMethods BufferAsSeq;
4414static PyMappingMethods BufferAsMapping;
4415
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004416 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004417BufferNew(buf_T *buf)
4418{
4419 /* We need to handle deletion of buffers underneath us.
4420 * If we add a "b_python*_ref" field to the buf_T structure,
4421 * then we can get at it in buf_freeall() in vim. We then
4422 * need to create only ONE Python object per buffer - if
4423 * we try to create a second, just INCREF the existing one
4424 * and return it. The (single) Python object referring to
4425 * the buffer is stored in "b_python*_ref".
4426 * Question: what to do on a buf_freeall(). We'll probably
4427 * have to either delete the Python object (DECREF it to
4428 * zero - a bad idea, as it leaves dangling refs!) or
4429 * set the buf_T * value to an invalid value (-1?), which
4430 * means we need checks in all access functions... Bah.
4431 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004432 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004433 * b_python_ref and b_python3_ref fields respectively.
4434 */
4435
4436 BufferObject *self;
4437
4438 if (BUF_PYTHON_REF(buf) != NULL)
4439 {
4440 self = BUF_PYTHON_REF(buf);
4441 Py_INCREF(self);
4442 }
4443 else
4444 {
4445 self = PyObject_NEW(BufferObject, &BufferType);
4446 if (self == NULL)
4447 return NULL;
4448 self->buf = buf;
4449 BUF_PYTHON_REF(buf) = self;
4450 }
4451
4452 return (PyObject *)(self);
4453}
4454
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004455 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004456BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004457{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004458 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4459 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004460
4461 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004462}
4463
Bram Moolenaar971db462013-05-12 18:44:48 +02004464 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004465BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004466{
4467 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004468 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004469 return -1; /* ??? */
4470
Bram Moolenaard6e39182013-05-21 18:30:34 +02004471 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004472}
4473
4474 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004475BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004476{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004477 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004478}
4479
4480 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004481BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004482{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004483 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004484}
4485
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004486static char *BufferAttrs[] = {
4487 "name", "number", "vars", "options", "valid",
4488 NULL
4489};
4490
4491 static PyObject *
4492BufferDir(PyObject *self)
4493{
4494 return ObjectDir(self, BufferAttrs);
4495}
4496
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004497 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004498BufferAttrValid(BufferObject *self, char *name)
4499{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004500 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004501
4502 if (strcmp(name, "valid") != 0)
4503 return NULL;
4504
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004505 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4506 Py_INCREF(ret);
4507 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004508}
4509
4510 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004511BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004512{
4513 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004514 return PyString_FromString((self->buf->b_ffname == NULL
4515 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004516 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004517 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004518 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004519 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004520 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004521 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4522 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004523 else if (strcmp(name, "__members__") == 0)
4524 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004525 else
4526 return NULL;
4527}
4528
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004529 static int
4530BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4531{
4532 if (CheckBuffer(self))
4533 return -1;
4534
4535 if (strcmp(name, "name") == 0)
4536 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004537 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004538 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004539 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004540 PyObject *todecref;
4541
4542 if (!(val = StringToChars(valObject, &todecref)))
4543 return -1;
4544
4545 VimTryStart();
4546 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4547 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004548 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004549 aucmd_restbuf(&aco);
4550 Py_XDECREF(todecref);
4551 if (VimTryEnd())
4552 return -1;
4553
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004554 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004555 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004556 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004557 return -1;
4558 }
4559 return 0;
4560 }
4561 else
4562 {
4563 PyErr_SetString(PyExc_AttributeError, name);
4564 return -1;
4565 }
4566}
4567
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004568 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004569BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004570{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004571 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572}
4573
4574 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004575BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004576{
4577 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004578 char_u *pmark;
4579 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004580 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004581 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004582
Bram Moolenaard6e39182013-05-21 18:30:34 +02004583 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004584 return NULL;
4585
Bram Moolenaar389a1792013-06-23 13:00:44 +02004586 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004587 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588
Bram Moolenaar389a1792013-06-23 13:00:44 +02004589 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004590 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004591 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004592 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004593 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004594 return NULL;
4595 }
4596
4597 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004598
4599 Py_XDECREF(todecref);
4600
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004601 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004602 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004604 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004605 if (VimTryEnd())
4606 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004607
4608 if (posp == NULL)
4609 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004610 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004611 return NULL;
4612 }
4613
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004614 if (posp->lnum <= 0)
4615 {
4616 /* Or raise an error? */
4617 Py_INCREF(Py_None);
4618 return Py_None;
4619 }
4620
4621 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4622}
4623
4624 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004625BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004626{
4627 PyInt start;
4628 PyInt end;
4629
Bram Moolenaard6e39182013-05-21 18:30:34 +02004630 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004631 return NULL;
4632
4633 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4634 return NULL;
4635
Bram Moolenaard6e39182013-05-21 18:30:34 +02004636 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004637}
4638
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004639 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004640BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004641{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004642 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004643 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004644 else
4645 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004646 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004647
4648 if (name == NULL)
4649 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004650
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004651 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004652 }
4653}
4654
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004655static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004656 /* name, function, calling, documentation */
4657 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004658 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004659 {"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 +02004660 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4661 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004662};
4663
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004664/*
4665 * Buffer list object - Implementation
4666 */
4667
4668static PyTypeObject BufMapType;
4669
4670typedef struct
4671{
4672 PyObject_HEAD
4673} BufMapObject;
4674
4675 static PyInt
4676BufMapLength(PyObject *self UNUSED)
4677{
4678 buf_T *b = firstbuf;
4679 PyInt n = 0;
4680
4681 while (b)
4682 {
4683 ++n;
4684 b = b->b_next;
4685 }
4686
4687 return n;
4688}
4689
4690 static PyObject *
4691BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4692{
4693 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004694 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004695
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004696 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004697 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004698
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004699 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004700
4701 if (b)
4702 return BufferNew(b);
4703 else
4704 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004705 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004706 return NULL;
4707 }
4708}
4709
4710 static void
4711BufMapIterDestruct(PyObject *buffer)
4712{
4713 /* Iteration was stopped before all buffers were processed */
4714 if (buffer)
4715 {
4716 Py_DECREF(buffer);
4717 }
4718}
4719
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004720 static int
4721BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4722{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004723 if (buffer)
4724 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004725 return 0;
4726}
4727
4728 static int
4729BufMapIterClear(PyObject **buffer)
4730{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004731 if (*buffer)
4732 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004733 return 0;
4734}
4735
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004736 static PyObject *
4737BufMapIterNext(PyObject **buffer)
4738{
4739 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004740 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004741
4742 if (!*buffer)
4743 return NULL;
4744
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004745 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004746
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004747 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004748 {
4749 *buffer = NULL;
4750 return NULL;
4751 }
4752
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004753 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004754 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004755 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004756 return NULL;
4757 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004758 /* Do not increment reference: we no longer hold it (decref), but whoever
4759 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004760 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004761}
4762
4763 static PyObject *
4764BufMapIter(PyObject *self UNUSED)
4765{
4766 PyObject *buffer;
4767
4768 buffer = BufferNew(firstbuf);
4769 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004770 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4771 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004772}
4773
4774static PyMappingMethods BufMapAsMapping = {
4775 (lenfunc) BufMapLength,
4776 (binaryfunc) BufMapItem,
4777 (objobjargproc) 0,
4778};
4779
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004780/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004781 */
4782
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004783static char *CurrentAttrs[] = {
4784 "buffer", "window", "line", "range", "tabpage",
4785 NULL
4786};
4787
4788 static PyObject *
4789CurrentDir(PyObject *self)
4790{
4791 return ObjectDir(self, CurrentAttrs);
4792}
4793
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004794 static PyObject *
4795CurrentGetattr(PyObject *self UNUSED, char *name)
4796{
4797 if (strcmp(name, "buffer") == 0)
4798 return (PyObject *)BufferNew(curbuf);
4799 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004800 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004801 else if (strcmp(name, "tabpage") == 0)
4802 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004803 else if (strcmp(name, "line") == 0)
4804 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4805 else if (strcmp(name, "range") == 0)
4806 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004807 else if (strcmp(name, "__members__") == 0)
4808 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004809 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004810#if PY_MAJOR_VERSION < 3
4811 return Py_FindMethod(WindowMethods, self, name);
4812#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004813 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004814#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004815}
4816
4817 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004818CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004819{
4820 if (strcmp(name, "line") == 0)
4821 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004822 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4823 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004824 return -1;
4825
4826 return 0;
4827 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004828 else if (strcmp(name, "buffer") == 0)
4829 {
4830 int count;
4831
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004832 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004833 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004834 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004835 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004836 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004837 return -1;
4838 }
4839
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004840 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004841 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004842 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004843
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004844 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004845 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4846 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004847 if (VimTryEnd())
4848 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004849 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004850 return -1;
4851 }
4852
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004853 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004854 }
4855 else if (strcmp(name, "window") == 0)
4856 {
4857 int count;
4858
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004859 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004860 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004861 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004862 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004863 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004864 return -1;
4865 }
4866
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004867 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004868 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004869 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004870
4871 if (!count)
4872 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004873 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004874 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004875 return -1;
4876 }
4877
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004878 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004879 win_goto(((WindowObject *)(valObject))->win);
4880 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004881 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004882 if (VimTryEnd())
4883 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004884 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004885 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004886 return -1;
4887 }
4888
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004889 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004890 }
4891 else if (strcmp(name, "tabpage") == 0)
4892 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004893 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004894 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004895 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004896 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004898 return -1;
4899 }
4900
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004902 return -1;
4903
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004904 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004905 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4906 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004907 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004908 if (VimTryEnd())
4909 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004910 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004911 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004912 return -1;
4913 }
4914
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004915 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004916 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004917 else
4918 {
4919 PyErr_SetString(PyExc_AttributeError, name);
4920 return -1;
4921 }
4922}
4923
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004924static struct PyMethodDef CurrentMethods[] = {
4925 /* name, function, calling, documentation */
4926 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4927 { NULL, NULL, 0, NULL}
4928};
4929
Bram Moolenaardb913952012-06-29 12:54:53 +02004930 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004931init_range_cmd(exarg_T *eap)
4932{
4933 RangeStart = eap->line1;
4934 RangeEnd = eap->line2;
4935}
4936
4937 static void
4938init_range_eval(typval_T *rettv UNUSED)
4939{
4940 RangeStart = (PyInt) curwin->w_cursor.lnum;
4941 RangeEnd = RangeStart;
4942}
4943
4944 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004945run_cmd(const char *cmd, void *arg UNUSED
4946#ifdef PY_CAN_RECURSE
4947 , PyGILState_STATE *pygilstate UNUSED
4948#endif
4949 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004950{
4951 PyRun_SimpleString((char *) cmd);
4952}
4953
4954static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4955static int code_hdr_len = 30;
4956
4957 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004958run_do(const char *cmd, void *arg UNUSED
4959#ifdef PY_CAN_RECURSE
4960 , PyGILState_STATE *pygilstate
4961#endif
4962 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004963{
4964 PyInt lnum;
4965 size_t len;
4966 char *code;
4967 int status;
4968 PyObject *pyfunc, *pymain;
4969
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004970 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004971 {
4972 EMSG(_("cannot save undo information"));
4973 return;
4974 }
4975
4976 len = code_hdr_len + STRLEN(cmd);
4977 code = PyMem_New(char, len + 1);
4978 memcpy(code, code_hdr, code_hdr_len);
4979 STRCPY(code + code_hdr_len, cmd);
4980 status = PyRun_SimpleString(code);
4981 PyMem_Free(code);
4982
4983 if (status)
4984 {
4985 EMSG(_("failed to run the code"));
4986 return;
4987 }
4988
4989 status = 0;
4990 pymain = PyImport_AddModule("__main__");
4991 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004992#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004993 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004994#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004995
4996 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4997 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004998 PyObject *line;
4999 PyObject *linenr;
5000 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005001
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005002#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005003 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005004#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005005 if (!(line = GetBufferLine(curbuf, lnum)))
5006 goto err;
5007 if (!(linenr = PyInt_FromLong((long) lnum)))
5008 {
5009 Py_DECREF(line);
5010 goto err;
5011 }
5012 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5013 Py_DECREF(line);
5014 Py_DECREF(linenr);
5015 if (!ret)
5016 goto err;
5017
5018 if (ret != Py_None)
5019 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5020 goto err;
5021
5022 Py_XDECREF(ret);
5023 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005024#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005025 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005026#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005027 }
5028 goto out;
5029err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005030#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005031 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005032#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005033 PyErr_PrintEx(0);
5034 PythonIO_Flush();
5035 status = 1;
5036out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005037#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005038 if (!status)
5039 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005040#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005041 Py_DECREF(pyfunc);
5042 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5043 if (status)
5044 return;
5045 check_cursor();
5046 update_curbuf(NOT_VALID);
5047}
5048
5049 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005050run_eval(const char *cmd, typval_T *rettv
5051#ifdef PY_CAN_RECURSE
5052 , PyGILState_STATE *pygilstate UNUSED
5053#endif
5054 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005055{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005056 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005057
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005058 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5059 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005060 {
5061 if (PyErr_Occurred() && !msg_silent)
5062 PyErr_PrintEx(0);
5063 EMSG(_("E858: Eval did not return a valid python object"));
5064 }
5065 else
5066 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005067 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005068 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005069 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005070 }
5071 PyErr_Clear();
5072}
5073
5074 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005075set_ref_in_py(const int copyID)
5076{
5077 pylinkedlist_T *cur;
5078 dict_T *dd;
5079 list_T *ll;
5080
5081 if (lastdict != NULL)
5082 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5083 {
5084 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5085 if (dd->dv_copyID != copyID)
5086 {
5087 dd->dv_copyID = copyID;
5088 set_ref_in_ht(&dd->dv_hashtab, copyID);
5089 }
5090 }
5091
5092 if (lastlist != NULL)
5093 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5094 {
5095 ll = ((ListObject *) (cur->pll_obj))->list;
5096 if (ll->lv_copyID != copyID)
5097 {
5098 ll->lv_copyID = copyID;
5099 set_ref_in_list(ll, copyID);
5100 }
5101 }
5102}
5103
5104 static int
5105set_string_copy(char_u *str, typval_T *tv)
5106{
5107 tv->vval.v_string = vim_strsave(str);
5108 if (tv->vval.v_string == NULL)
5109 {
5110 PyErr_NoMemory();
5111 return -1;
5112 }
5113 return 0;
5114}
5115
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005117pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005119 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005120 char_u *key;
5121 dictitem_T *di;
5122 PyObject *keyObject;
5123 PyObject *valObject;
5124 Py_ssize_t iter = 0;
5125
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005126 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005128
5129 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005130 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131
5132 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5133 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005134 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135
Bram Moolenaara03e6312013-05-29 22:49:26 +02005136 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005137 {
5138 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005140 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005141
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005142 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005143 {
5144 dict_unref(dict);
5145 return -1;
5146 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005147
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005148 if (*key == NUL)
5149 {
5150 dict_unref(dict);
5151 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005152 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005153 return -1;
5154 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005155
5156 di = dictitem_alloc(key);
5157
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005158 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005159
5160 if (di == NULL)
5161 {
5162 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005163 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005164 return -1;
5165 }
5166 di->di_tv.v_lock = 0;
5167
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005168 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 {
5170 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005171 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172 return -1;
5173 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005174
5175 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005176 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005177 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005178 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005179 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005180 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005181 return -1;
5182 }
5183 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005184
5185 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005186 return 0;
5187}
5188
5189 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005190pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005191{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005192 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 char_u *key;
5194 dictitem_T *di;
5195 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005196 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197 PyObject *keyObject;
5198 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005199
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005200 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005201 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005202
5203 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005204 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005205
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005206 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005207 {
5208 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005209 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005210 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005211
5212 if (!(iterator = PyObject_GetIter(list)))
5213 {
5214 dict_unref(dict);
5215 Py_DECREF(list);
5216 return -1;
5217 }
5218 Py_DECREF(list);
5219
5220 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005221 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005222 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005223
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005224 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005225 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005226 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005227 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005228 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005229 return -1;
5230 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005231
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005232 if (*key == NUL)
5233 {
5234 Py_DECREF(keyObject);
5235 Py_DECREF(iterator);
5236 Py_XDECREF(todecref);
5237 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005238 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005239 return -1;
5240 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005241
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005242 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005243 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005244 Py_DECREF(keyObject);
5245 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005246 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005247 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005248 return -1;
5249 }
5250
5251 di = dictitem_alloc(key);
5252
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005253 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005254 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005255
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 if (di == NULL)
5257 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005258 Py_DECREF(iterator);
5259 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005260 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005261 PyErr_NoMemory();
5262 return -1;
5263 }
5264 di->di_tv.v_lock = 0;
5265
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005266 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005268 Py_DECREF(iterator);
5269 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005270 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005271 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005272 return -1;
5273 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005274
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005275 Py_DECREF(valObject);
5276
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005277 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005278 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005279 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005280 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005281 dictitem_free(di);
5282 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005283 return -1;
5284 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005285 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005286 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005287 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005288 return 0;
5289}
5290
5291 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005292pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293{
5294 list_T *l;
5295
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005296 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005297 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005298
5299 tv->v_type = VAR_LIST;
5300 tv->vval.v_list = l;
5301
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005302 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005303 {
5304 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005305 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005306 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005307
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005308 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005309 return 0;
5310}
5311
Bram Moolenaardb913952012-06-29 12:54:53 +02005312typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5313
5314 static int
5315convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005316 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005317{
5318 PyObject *capsule;
5319 char hexBuf[sizeof(void *) * 2 + 3];
5320
5321 sprintf(hexBuf, "%p", obj);
5322
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005323# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005324 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005325# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005326 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005327# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005328 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005329 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005330# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005331 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005332# else
5333 capsule = PyCObject_FromVoidPtr(tv, NULL);
5334# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005335 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5336 {
5337 Py_DECREF(capsule);
5338 tv->v_type = VAR_UNKNOWN;
5339 return -1;
5340 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005341
5342 Py_DECREF(capsule);
5343
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005344 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005345 {
5346 tv->v_type = VAR_UNKNOWN;
5347 return -1;
5348 }
5349 /* As we are not using copy_tv which increments reference count we must
5350 * do it ourself. */
5351 switch(tv->v_type)
5352 {
5353 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5354 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5355 }
5356 }
5357 else
5358 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005359 typval_T *v;
5360
5361# ifdef PY_USE_CAPSULE
5362 v = PyCapsule_GetPointer(capsule, NULL);
5363# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005364 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005365# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005366 copy_tv(v, tv);
5367 }
5368 return 0;
5369}
5370
5371 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005372ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5373{
5374 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005375 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005376
5377 if (!(lookup_dict = PyDict_New()))
5378 return -1;
5379
5380 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5381 {
5382 tv->v_type = VAR_DICT;
5383 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5384 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005385 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005386 }
5387 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005388 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005389 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005390 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005391 else
5392 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005393 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005394 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005395 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005396 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005397 }
5398 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005399 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005400}
5401
5402 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005403ConvertFromPyObject(PyObject *obj, typval_T *tv)
5404{
5405 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005406 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005407
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005408 if (!(lookup_dict = PyDict_New()))
5409 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005410 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005411 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005412 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005413}
5414
5415 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005416_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005417{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005418 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005419 {
5420 tv->v_type = VAR_DICT;
5421 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5422 ++tv->vval.v_dict->dv_refcount;
5423 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005424 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005425 {
5426 tv->v_type = VAR_LIST;
5427 tv->vval.v_list = (((ListObject *)(obj))->list);
5428 ++tv->vval.v_list->lv_refcount;
5429 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005430 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005431 {
5432 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5433 return -1;
5434
5435 tv->v_type = VAR_FUNC;
5436 func_ref(tv->vval.v_string);
5437 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005438 else if (PyBytes_Check(obj))
5439 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005440 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005441
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005442 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005443 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005444 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005445 return -1;
5446
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005448 return -1;
5449
5450 tv->v_type = VAR_STRING;
5451 }
5452 else if (PyUnicode_Check(obj))
5453 {
5454 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005455 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005456
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005457 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005458 if (bytes == NULL)
5459 return -1;
5460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005462 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005463 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005464 return -1;
5465
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005466 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005467 {
5468 Py_XDECREF(bytes);
5469 return -1;
5470 }
5471 Py_XDECREF(bytes);
5472
5473 tv->v_type = VAR_STRING;
5474 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005475#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005476 else if (PyInt_Check(obj))
5477 {
5478 tv->v_type = VAR_NUMBER;
5479 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005480 if (PyErr_Occurred())
5481 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005482 }
5483#endif
5484 else if (PyLong_Check(obj))
5485 {
5486 tv->v_type = VAR_NUMBER;
5487 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005488 if (PyErr_Occurred())
5489 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005490 }
5491 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005492 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005493#ifdef FEAT_FLOAT
5494 else if (PyFloat_Check(obj))
5495 {
5496 tv->v_type = VAR_FLOAT;
5497 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5498 }
5499#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005500 else if (PyObject_HasAttrString(obj, "keys"))
5501 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005502 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005503 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005504 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005505 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005506 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005507 else if (PyNumber_Check(obj))
5508 {
5509 PyObject *num;
5510
5511 if (!(num = PyNumber_Long(obj)))
5512 return -1;
5513
5514 tv->v_type = VAR_NUMBER;
5515 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5516
5517 Py_DECREF(num);
5518 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005519 else
5520 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005521 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005522 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005523 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005524 return -1;
5525 }
5526 return 0;
5527}
5528
5529 static PyObject *
5530ConvertToPyObject(typval_T *tv)
5531{
5532 if (tv == NULL)
5533 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005534 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005535 return NULL;
5536 }
5537 switch (tv->v_type)
5538 {
5539 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005540 return PyBytes_FromString(tv->vval.v_string == NULL
5541 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005542 case VAR_NUMBER:
5543 return PyLong_FromLong((long) tv->vval.v_number);
5544#ifdef FEAT_FLOAT
5545 case VAR_FLOAT:
5546 return PyFloat_FromDouble((double) tv->vval.v_float);
5547#endif
5548 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005549 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005550 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005551 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005552 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005553 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005554 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005555 case VAR_UNKNOWN:
5556 Py_INCREF(Py_None);
5557 return Py_None;
5558 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005559 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005560 return NULL;
5561 }
5562}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005563
5564typedef struct
5565{
5566 PyObject_HEAD
5567} CurrentObject;
5568static PyTypeObject CurrentType;
5569
5570 static void
5571init_structs(void)
5572{
5573 vim_memset(&OutputType, 0, sizeof(OutputType));
5574 OutputType.tp_name = "vim.message";
5575 OutputType.tp_basicsize = sizeof(OutputObject);
5576 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5577 OutputType.tp_doc = "vim message object";
5578 OutputType.tp_methods = OutputMethods;
5579#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005580 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5581 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005582 OutputType.tp_alloc = call_PyType_GenericAlloc;
5583 OutputType.tp_new = call_PyType_GenericNew;
5584 OutputType.tp_free = call_PyObject_Free;
5585#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005586 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5587 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005588#endif
5589
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005590 vim_memset(&IterType, 0, sizeof(IterType));
5591 IterType.tp_name = "vim.iter";
5592 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005593 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005594 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005595 IterType.tp_iter = (getiterfunc)IterIter;
5596 IterType.tp_iternext = (iternextfunc)IterNext;
5597 IterType.tp_dealloc = (destructor)IterDestructor;
5598 IterType.tp_traverse = (traverseproc)IterTraverse;
5599 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005600
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005601 vim_memset(&BufferType, 0, sizeof(BufferType));
5602 BufferType.tp_name = "vim.buffer";
5603 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005604 BufferType.tp_dealloc = (destructor)BufferDestructor;
5605 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005606 BufferType.tp_as_sequence = &BufferAsSeq;
5607 BufferType.tp_as_mapping = &BufferAsMapping;
5608 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5609 BufferType.tp_doc = "vim buffer object";
5610 BufferType.tp_methods = BufferMethods;
5611#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005612 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005613 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005614 BufferType.tp_alloc = call_PyType_GenericAlloc;
5615 BufferType.tp_new = call_PyType_GenericNew;
5616 BufferType.tp_free = call_PyObject_Free;
5617#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005618 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005619 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005620#endif
5621
5622 vim_memset(&WindowType, 0, sizeof(WindowType));
5623 WindowType.tp_name = "vim.window";
5624 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005625 WindowType.tp_dealloc = (destructor)WindowDestructor;
5626 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005627 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005628 WindowType.tp_doc = "vim Window object";
5629 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005630 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5631 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005633 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5634 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005635 WindowType.tp_alloc = call_PyType_GenericAlloc;
5636 WindowType.tp_new = call_PyType_GenericNew;
5637 WindowType.tp_free = call_PyObject_Free;
5638#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005639 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5640 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005641#endif
5642
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005643 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5644 TabPageType.tp_name = "vim.tabpage";
5645 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005646 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5647 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005648 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5649 TabPageType.tp_doc = "vim tab page object";
5650 TabPageType.tp_methods = TabPageMethods;
5651#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005652 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005653 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5654 TabPageType.tp_new = call_PyType_GenericNew;
5655 TabPageType.tp_free = call_PyObject_Free;
5656#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005657 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005658#endif
5659
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005660 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5661 BufMapType.tp_name = "vim.bufferlist";
5662 BufMapType.tp_basicsize = sizeof(BufMapObject);
5663 BufMapType.tp_as_mapping = &BufMapAsMapping;
5664 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005665 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005666 BufferType.tp_doc = "vim buffer list";
5667
5668 vim_memset(&WinListType, 0, sizeof(WinListType));
5669 WinListType.tp_name = "vim.windowlist";
5670 WinListType.tp_basicsize = sizeof(WinListType);
5671 WinListType.tp_as_sequence = &WinListAsSeq;
5672 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5673 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005674 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005675
5676 vim_memset(&TabListType, 0, sizeof(TabListType));
5677 TabListType.tp_name = "vim.tabpagelist";
5678 TabListType.tp_basicsize = sizeof(TabListType);
5679 TabListType.tp_as_sequence = &TabListAsSeq;
5680 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5681 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005682
5683 vim_memset(&RangeType, 0, sizeof(RangeType));
5684 RangeType.tp_name = "vim.range";
5685 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005686 RangeType.tp_dealloc = (destructor)RangeDestructor;
5687 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005688 RangeType.tp_as_sequence = &RangeAsSeq;
5689 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005690 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005691 RangeType.tp_doc = "vim Range object";
5692 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005693 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5694 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005695#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005696 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005697 RangeType.tp_alloc = call_PyType_GenericAlloc;
5698 RangeType.tp_new = call_PyType_GenericNew;
5699 RangeType.tp_free = call_PyObject_Free;
5700#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005701 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005702#endif
5703
5704 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5705 CurrentType.tp_name = "vim.currentdata";
5706 CurrentType.tp_basicsize = sizeof(CurrentObject);
5707 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5708 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005709 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005710#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005711 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5712 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005713#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005714 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5715 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005716#endif
5717
5718 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5719 DictionaryType.tp_name = "vim.dictionary";
5720 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005721 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005722 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005723 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005724 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005725 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5726 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005727 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5728 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5729 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005730#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005731 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5732 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005733#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005734 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5735 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005736#endif
5737
5738 vim_memset(&ListType, 0, sizeof(ListType));
5739 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005740 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005741 ListType.tp_basicsize = sizeof(ListObject);
5742 ListType.tp_as_sequence = &ListAsSeq;
5743 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005744 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005745 ListType.tp_doc = "list pushing modifications to vim structure";
5746 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005747 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005748 ListType.tp_new = (newfunc)ListConstructor;
5749 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005750#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005751 ListType.tp_getattro = (getattrofunc)ListGetattro;
5752 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005753#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005754 ListType.tp_getattr = (getattrfunc)ListGetattr;
5755 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005756#endif
5757
5758 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005759 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005760 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005761 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5762 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005763 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005764 FunctionType.tp_doc = "object that calls vim function";
5765 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005766 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005767 FunctionType.tp_new = (newfunc)FunctionConstructor;
5768 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005769#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005770 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005771#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005772 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005773#endif
5774
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005775 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5776 OptionsType.tp_name = "vim.options";
5777 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005778 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005779 OptionsType.tp_doc = "object for manipulating options";
5780 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005781 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5782 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5783 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005784
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005785 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5786 LoaderType.tp_name = "vim.Loader";
5787 LoaderType.tp_basicsize = sizeof(LoaderObject);
5788 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5789 LoaderType.tp_doc = "vim message object";
5790 LoaderType.tp_methods = LoaderMethods;
5791 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5792
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005793#if PY_MAJOR_VERSION >= 3
5794 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5795 vimmodule.m_name = "vim";
5796 vimmodule.m_doc = "Vim Python interface\n";
5797 vimmodule.m_size = -1;
5798 vimmodule.m_methods = VimMethods;
5799#endif
5800}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005801
5802#define PYTYPE_READY(type) \
5803 if (PyType_Ready(&type)) \
5804 return -1;
5805
5806 static int
5807init_types()
5808{
5809 PYTYPE_READY(IterType);
5810 PYTYPE_READY(BufferType);
5811 PYTYPE_READY(RangeType);
5812 PYTYPE_READY(WindowType);
5813 PYTYPE_READY(TabPageType);
5814 PYTYPE_READY(BufMapType);
5815 PYTYPE_READY(WinListType);
5816 PYTYPE_READY(TabListType);
5817 PYTYPE_READY(CurrentType);
5818 PYTYPE_READY(DictionaryType);
5819 PYTYPE_READY(ListType);
5820 PYTYPE_READY(FunctionType);
5821 PYTYPE_READY(OptionsType);
5822 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005823 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005824 return 0;
5825}
5826
5827 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005828init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005829{
5830 PyObject *path;
5831 PyObject *path_hook;
5832 PyObject *path_hooks;
5833
5834 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5835 return -1;
5836
5837 if (!(path_hooks = PySys_GetObject("path_hooks")))
5838 {
5839 PyErr_Clear();
5840 path_hooks = PyList_New(1);
5841 PyList_SET_ITEM(path_hooks, 0, path_hook);
5842 if (PySys_SetObject("path_hooks", path_hooks))
5843 {
5844 Py_DECREF(path_hooks);
5845 return -1;
5846 }
5847 Py_DECREF(path_hooks);
5848 }
5849 else if (PyList_Check(path_hooks))
5850 {
5851 if (PyList_Append(path_hooks, path_hook))
5852 {
5853 Py_DECREF(path_hook);
5854 return -1;
5855 }
5856 Py_DECREF(path_hook);
5857 }
5858 else
5859 {
5860 VimTryStart();
5861 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5862 "You should now do the following:\n"
5863 "- append vim.path_hook to sys.path_hooks\n"
5864 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5865 VimTryEnd(); /* Discard the error */
5866 Py_DECREF(path_hook);
5867 return 0;
5868 }
5869
5870 if (!(path = PySys_GetObject("path")))
5871 {
5872 PyErr_Clear();
5873 path = PyList_New(1);
5874 Py_INCREF(vim_special_path_object);
5875 PyList_SET_ITEM(path, 0, vim_special_path_object);
5876 if (PySys_SetObject("path", path))
5877 {
5878 Py_DECREF(path);
5879 return -1;
5880 }
5881 Py_DECREF(path);
5882 }
5883 else if (PyList_Check(path))
5884 {
5885 if (PyList_Append(path, vim_special_path_object))
5886 return -1;
5887 }
5888 else
5889 {
5890 VimTryStart();
5891 EMSG(_("Failed to set path: sys.path is not a list\n"
5892 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5893 VimTryEnd(); /* Discard the error */
5894 }
5895
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005896 return 0;
5897}
5898
5899static BufMapObject TheBufferMap =
5900{
5901 PyObject_HEAD_INIT(&BufMapType)
5902};
5903
5904static WinListObject TheWindowList =
5905{
5906 PyObject_HEAD_INIT(&WinListType)
5907 NULL
5908};
5909
5910static CurrentObject TheCurrent =
5911{
5912 PyObject_HEAD_INIT(&CurrentType)
5913};
5914
5915static TabListObject TheTabPageList =
5916{
5917 PyObject_HEAD_INIT(&TabListType)
5918};
5919
5920static struct numeric_constant {
5921 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005922 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005923} numeric_constants[] = {
5924 {"VAR_LOCKED", VAR_LOCKED},
5925 {"VAR_FIXED", VAR_FIXED},
5926 {"VAR_SCOPE", VAR_SCOPE},
5927 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5928};
5929
5930static struct object_constant {
5931 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005932 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005933} object_constants[] = {
5934 {"buffers", (PyObject *)(void *)&TheBufferMap},
5935 {"windows", (PyObject *)(void *)&TheWindowList},
5936 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5937 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005938
5939 {"Buffer", (PyObject *)&BufferType},
5940 {"Range", (PyObject *)&RangeType},
5941 {"Window", (PyObject *)&WindowType},
5942 {"TabPage", (PyObject *)&TabPageType},
5943 {"Dictionary", (PyObject *)&DictionaryType},
5944 {"List", (PyObject *)&ListType},
5945 {"Function", (PyObject *)&FunctionType},
5946 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005947 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005948};
5949
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005950#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02005951 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005952 return -1;
5953
5954#define ADD_CHECKED_OBJECT(m, name, obj) \
5955 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005956 PyObject *valObject = obj; \
5957 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005958 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005959 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005960 }
5961
5962 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02005963populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005964{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005965 int i;
5966 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005967 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005968 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005969
5970 for (i = 0; i < (int)(sizeof(numeric_constants)
5971 / sizeof(struct numeric_constant));
5972 ++i)
5973 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005974 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005975
5976 for (i = 0; i < (int)(sizeof(object_constants)
5977 / sizeof(struct object_constant));
5978 ++i)
5979 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005980 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005981
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005982 valObject = object_constants[i].valObject;
5983 Py_INCREF(valObject);
5984 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005985 }
5986
5987 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5988 return -1;
5989 ADD_OBJECT(m, "error", VimError);
5990
Bram Moolenaara9922d62013-05-30 13:01:18 +02005991 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5992 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005993 ADD_CHECKED_OBJECT(m, "options",
5994 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005995
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005996 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005997 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005998 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005999
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006000 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006001 return -1;
6002 ADD_OBJECT(m, "_getcwd", py_getcwd)
6003
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006004 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006005 return -1;
6006 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006007 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006008 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006009 if (PyObject_SetAttrString(other_module, "chdir", attr))
6010 {
6011 Py_DECREF(attr);
6012 return -1;
6013 }
6014 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006015
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006016 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006017 {
6018 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006019 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006020 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006021 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6022 {
6023 Py_DECREF(attr);
6024 return -1;
6025 }
6026 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006027 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006028 else
6029 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006030
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006031 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6032 return -1;
6033
6034 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6035
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006036 if (!(imp = PyImport_ImportModule("imp")))
6037 return -1;
6038
6039 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6040 {
6041 Py_DECREF(imp);
6042 return -1;
6043 }
6044
6045 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6046 {
6047 Py_DECREF(py_find_module);
6048 Py_DECREF(imp);
6049 return -1;
6050 }
6051
6052 Py_DECREF(imp);
6053
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006054 ADD_OBJECT(m, "_find_module", py_find_module);
6055 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006056
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006057 return 0;
6058}