blob: b564d24b5d623220865b740ac0b28d610f461389 [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 Moolenaardaa27022013-06-24 22:33:30 +02003552 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003553 || str == NULL)
3554 {
3555 Py_DECREF(bytes);
3556 return NULL;
3557 }
3558 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003559 else
3560 {
3561#if PY_MAJOR_VERSION < 3
3562 PyErr_FORMAT(PyExc_TypeError,
3563 N_("expected str() or unicode() instance, but got %s"),
3564 Py_TYPE_NAME(obj));
3565#else
3566 PyErr_FORMAT(PyExc_TypeError,
3567 N_("expected bytes() or str() instance, but got %s"),
3568 Py_TYPE_NAME(obj));
3569#endif
3570 return NULL;
3571 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003572
3573 /*
3574 * Error checking: String must not contain newlines, as we
3575 * are replacing a single line, and we must replace it with
3576 * a single line.
3577 * A trailing newline is removed, so that append(f.readlines()) works.
3578 */
3579 p = memchr(str, '\n', len);
3580 if (p != NULL)
3581 {
3582 if (p == str + len - 1)
3583 --len;
3584 else
3585 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003586 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003587 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003588 return NULL;
3589 }
3590 }
3591
3592 /* Create a copy of the string, with internal nulls replaced by
3593 * newline characters, as is the vim convention.
3594 */
3595 save = (char *)alloc((unsigned)(len+1));
3596 if (save == NULL)
3597 {
3598 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003599 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003600 return NULL;
3601 }
3602
3603 for (i = 0; i < len; ++i)
3604 {
3605 if (str[i] == '\0')
3606 save[i] = '\n';
3607 else
3608 save[i] = str[i];
3609 }
3610
3611 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003612 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003613
3614 return save;
3615}
3616
3617/* Get a line from the specified buffer. The line number is
3618 * in Vim format (1-based). The line is returned as a Python
3619 * string object.
3620 */
3621 static PyObject *
3622GetBufferLine(buf_T *buf, PyInt n)
3623{
3624 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3625}
3626
3627
3628/* Get a list of lines from the specified buffer. The line numbers
3629 * are in Vim format (1-based). The range is from lo up to, but not
3630 * including, hi. The list is returned as a Python list of string objects.
3631 */
3632 static PyObject *
3633GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3634{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003635 PyInt i;
3636 PyInt n = hi - lo;
3637 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003638
3639 if (list == NULL)
3640 return NULL;
3641
3642 for (i = 0; i < n; ++i)
3643 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003644 PyObject *string = LineToString(
3645 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003646
3647 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003648 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003649 {
3650 Py_DECREF(list);
3651 return NULL;
3652 }
3653
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003654 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003655 }
3656
3657 /* The ownership of the Python list is passed to the caller (ie,
3658 * the caller should Py_DECREF() the object when it is finished
3659 * with it).
3660 */
3661
3662 return list;
3663}
3664
3665/*
3666 * Check if deleting lines made the cursor position invalid.
3667 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3668 * deleted).
3669 */
3670 static void
3671py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3672{
3673 if (curwin->w_cursor.lnum >= lo)
3674 {
3675 /* Adjust the cursor position if it's in/after the changed
3676 * lines. */
3677 if (curwin->w_cursor.lnum >= hi)
3678 {
3679 curwin->w_cursor.lnum += extra;
3680 check_cursor_col();
3681 }
3682 else if (extra < 0)
3683 {
3684 curwin->w_cursor.lnum = lo;
3685 check_cursor();
3686 }
3687 else
3688 check_cursor_col();
3689 changed_cline_bef_curs();
3690 }
3691 invalidate_botline();
3692}
3693
Bram Moolenaar19e60942011-06-19 00:27:51 +02003694/*
3695 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 * in Vim format (1-based). The replacement line is given as
3697 * a Python string object. The object is checked for validity
3698 * and correct format. Errors are returned as a value of FAIL.
3699 * The return value is OK on success.
3700 * If OK is returned and len_change is not NULL, *len_change
3701 * is set to the change in the buffer length.
3702 */
3703 static int
3704SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3705{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003706 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 * There are three cases:
3708 * 1. NULL, or None - this is a deletion.
3709 * 2. A string - this is a replacement.
3710 * 3. Anything else - this is an error.
3711 */
3712 if (line == Py_None || line == NULL)
3713 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003714 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715
3716 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003717 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003719 VimTryStart();
3720
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003722 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003723 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003724 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003725 else
3726 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003727 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003728 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3729 deleted_lines_mark((linenr_T)n, 1L);
3730 }
3731
Bram Moolenaar105bc352013-05-17 16:03:57 +02003732 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003733
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003734 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003735 return FAIL;
3736
3737 if (len_change)
3738 *len_change = -1;
3739
3740 return OK;
3741 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003742 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003743 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003744 char *save = StringToLine(line);
3745 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003746
3747 if (save == NULL)
3748 return FAIL;
3749
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003750 VimTryStart();
3751
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003752 /* We do not need to free "save" if ml_replace() consumes it. */
3753 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003754 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003755
3756 if (u_savesub((linenr_T)n) == FAIL)
3757 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003758 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759 vim_free(save);
3760 }
3761 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3762 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003763 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 vim_free(save);
3765 }
3766 else
3767 changed_bytes((linenr_T)n, 0);
3768
Bram Moolenaar105bc352013-05-17 16:03:57 +02003769 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003770
3771 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003772 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773 check_cursor_col();
3774
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003775 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 return FAIL;
3777
3778 if (len_change)
3779 *len_change = 0;
3780
3781 return OK;
3782 }
3783 else
3784 {
3785 PyErr_BadArgument();
3786 return FAIL;
3787 }
3788}
3789
Bram Moolenaar19e60942011-06-19 00:27:51 +02003790/* Replace a range of lines in the specified buffer. The line numbers are in
3791 * Vim format (1-based). The range is from lo up to, but not including, hi.
3792 * The replacement lines are given as a Python list of string objects. The
3793 * list is checked for validity and correct format. Errors are returned as a
3794 * value of FAIL. The return value is OK on success.
3795 * If OK is returned and len_change is not NULL, *len_change
3796 * is set to the change in the buffer length.
3797 */
3798 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003799SetBufferLineList(
3800 buf_T *buf,
3801 PyInt lo,
3802 PyInt hi,
3803 PyObject *list,
3804 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003805{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003806 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003807 * There are three cases:
3808 * 1. NULL, or None - this is a deletion.
3809 * 2. A list - this is a replacement.
3810 * 3. Anything else - this is an error.
3811 */
3812 if (list == Py_None || list == NULL)
3813 {
3814 PyInt i;
3815 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003816 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003817
3818 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003819 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003820 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003821
3822 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003823 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003824 else
3825 {
3826 for (i = 0; i < n; ++i)
3827 {
3828 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3829 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003830 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003831 break;
3832 }
3833 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003834 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003835 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3836 deleted_lines_mark((linenr_T)lo, (long)i);
3837 }
3838
Bram Moolenaar105bc352013-05-17 16:03:57 +02003839 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003840
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003841 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003842 return FAIL;
3843
3844 if (len_change)
3845 *len_change = -n;
3846
3847 return OK;
3848 }
3849 else if (PyList_Check(list))
3850 {
3851 PyInt i;
3852 PyInt new_len = PyList_Size(list);
3853 PyInt old_len = hi - lo;
3854 PyInt extra = 0; /* lines added to text, can be negative */
3855 char **array;
3856 buf_T *savebuf;
3857
3858 if (new_len == 0) /* avoid allocating zero bytes */
3859 array = NULL;
3860 else
3861 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003862 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003863 if (array == NULL)
3864 {
3865 PyErr_NoMemory();
3866 return FAIL;
3867 }
3868 }
3869
3870 for (i = 0; i < new_len; ++i)
3871 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003872 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003873
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003874 if (!(line = PyList_GetItem(list, i)) ||
3875 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003876 {
3877 while (i)
3878 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003879 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003880 return FAIL;
3881 }
3882 }
3883
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003884 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003885 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003886
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003887 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003888 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003889
3890 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003891 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003892
3893 /* If the size of the range is reducing (ie, new_len < old_len) we
3894 * need to delete some old_len. We do this at the start, by
3895 * repeatedly deleting line "lo".
3896 */
3897 if (!PyErr_Occurred())
3898 {
3899 for (i = 0; i < old_len - new_len; ++i)
3900 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3901 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003902 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003903 break;
3904 }
3905 extra -= i;
3906 }
3907
3908 /* For as long as possible, replace the existing old_len with the
3909 * new old_len. This is a more efficient operation, as it requires
3910 * less memory allocation and freeing.
3911 */
3912 if (!PyErr_Occurred())
3913 {
3914 for (i = 0; i < old_len && i < new_len; ++i)
3915 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3916 == FAIL)
3917 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003918 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003919 break;
3920 }
3921 }
3922 else
3923 i = 0;
3924
3925 /* Now we may need to insert the remaining new old_len. If we do, we
3926 * must free the strings as we finish with them (we can't pass the
3927 * responsibility to vim in this case).
3928 */
3929 if (!PyErr_Occurred())
3930 {
3931 while (i < new_len)
3932 {
3933 if (ml_append((linenr_T)(lo + i - 1),
3934 (char_u *)array[i], 0, FALSE) == FAIL)
3935 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003936 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003937 break;
3938 }
3939 vim_free(array[i]);
3940 ++i;
3941 ++extra;
3942 }
3943 }
3944
3945 /* Free any left-over old_len, as a result of an error */
3946 while (i < new_len)
3947 {
3948 vim_free(array[i]);
3949 ++i;
3950 }
3951
3952 /* Free the array of old_len. All of its contents have now
3953 * been dealt with (either freed, or the responsibility passed
3954 * to vim.
3955 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003956 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003957
3958 /* Adjust marks. Invalidate any which lie in the
3959 * changed range, and move any in the remainder of the buffer.
3960 */
3961 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3962 (long)MAXLNUM, (long)extra);
3963 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3964
Bram Moolenaar105bc352013-05-17 16:03:57 +02003965 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003966 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3967
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003968 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003969 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003970
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003971 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003972 return FAIL;
3973
3974 if (len_change)
3975 *len_change = new_len - old_len;
3976
3977 return OK;
3978 }
3979 else
3980 {
3981 PyErr_BadArgument();
3982 return FAIL;
3983 }
3984}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003985
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003986/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003987 * The line number is in Vim format (1-based). The lines to be inserted are
3988 * given as a Python list of string objects or as a single string. The lines
3989 * to be added are checked for validity and correct format. Errors are
3990 * returned as a value of FAIL. The return value is OK on success.
3991 * If OK is returned and len_change is not NULL, *len_change
3992 * is set to the change in the buffer length.
3993 */
3994 static int
3995InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3996{
3997 /* First of all, we check the type of the supplied Python object.
3998 * It must be a string or a list, or the call is in error.
3999 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004000 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 {
4002 char *str = StringToLine(lines);
4003 buf_T *savebuf;
4004
4005 if (str == NULL)
4006 return FAIL;
4007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004009 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004010 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011
4012 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004013 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004015 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 else
4017 appended_lines_mark((linenr_T)n, 1L);
4018
4019 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004020 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021 update_screen(VALID);
4022
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004023 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 return FAIL;
4025
4026 if (len_change)
4027 *len_change = 1;
4028
4029 return OK;
4030 }
4031 else if (PyList_Check(lines))
4032 {
4033 PyInt i;
4034 PyInt size = PyList_Size(lines);
4035 char **array;
4036 buf_T *savebuf;
4037
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004038 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 if (array == NULL)
4040 {
4041 PyErr_NoMemory();
4042 return FAIL;
4043 }
4044
4045 for (i = 0; i < size; ++i)
4046 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004047 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004049 if (!(line = PyList_GetItem(lines, i)) ||
4050 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 {
4052 while (i)
4053 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004054 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055 return FAIL;
4056 }
4057 }
4058
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004061 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062
4063 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004064 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 else
4066 {
4067 for (i = 0; i < size; ++i)
4068 {
4069 if (ml_append((linenr_T)(n + i),
4070 (char_u *)array[i], 0, FALSE) == FAIL)
4071 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004072 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073
4074 /* Free the rest of the lines */
4075 while (i < size)
4076 vim_free(array[i++]);
4077
4078 break;
4079 }
4080 vim_free(array[i]);
4081 }
4082 if (i > 0)
4083 appended_lines_mark((linenr_T)n, (long)i);
4084 }
4085
4086 /* Free the array of lines. All of its contents have now
4087 * been freed.
4088 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004089 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090
Bram Moolenaar105bc352013-05-17 16:03:57 +02004091 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 update_screen(VALID);
4093
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004094 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 return FAIL;
4096
4097 if (len_change)
4098 *len_change = size;
4099
4100 return OK;
4101 }
4102 else
4103 {
4104 PyErr_BadArgument();
4105 return FAIL;
4106 }
4107}
4108
4109/*
4110 * Common routines for buffers and line ranges
4111 * -------------------------------------------
4112 */
4113
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004114typedef struct
4115{
4116 PyObject_HEAD
4117 buf_T *buf;
4118} BufferObject;
4119
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004121CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004123 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004125 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 return -1;
4127 }
4128
4129 return 0;
4130}
4131
4132 static PyObject *
4133RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4134{
4135 if (CheckBuffer(self))
4136 return NULL;
4137
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004138 if (end == -1)
4139 end = self->buf->b_ml.ml_line_count;
4140
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004141 if (n < 0)
4142 n += end - start + 1;
4143
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144 if (n < 0 || n > end - start)
4145 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004146 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 return NULL;
4148 }
4149
4150 return GetBufferLine(self->buf, n+start);
4151}
4152
4153 static PyObject *
4154RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4155{
4156 PyInt size;
4157
4158 if (CheckBuffer(self))
4159 return NULL;
4160
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004161 if (end == -1)
4162 end = self->buf->b_ml.ml_line_count;
4163
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164 size = end - start + 1;
4165
4166 if (lo < 0)
4167 lo = 0;
4168 else if (lo > size)
4169 lo = size;
4170 if (hi < 0)
4171 hi = 0;
4172 if (hi < lo)
4173 hi = lo;
4174 else if (hi > size)
4175 hi = size;
4176
4177 return GetBufferLineList(self->buf, lo+start, hi+start);
4178}
4179
4180 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004181RBAsItem(
4182 BufferObject *self,
4183 PyInt n,
4184 PyObject *valObject,
4185 PyInt start,
4186 PyInt end,
4187 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188{
4189 PyInt len_change;
4190
4191 if (CheckBuffer(self))
4192 return -1;
4193
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004194 if (end == -1)
4195 end = self->buf->b_ml.ml_line_count;
4196
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004197 if (n < 0)
4198 n += end - start + 1;
4199
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004200 if (n < 0 || n > end - start)
4201 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004202 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004203 return -1;
4204 }
4205
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004206 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004207 return -1;
4208
4209 if (new_end)
4210 *new_end = end + len_change;
4211
4212 return 0;
4213}
4214
Bram Moolenaar19e60942011-06-19 00:27:51 +02004215 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004216RBAsSlice(
4217 BufferObject *self,
4218 PyInt lo,
4219 PyInt hi,
4220 PyObject *valObject,
4221 PyInt start,
4222 PyInt end,
4223 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004224{
4225 PyInt size;
4226 PyInt len_change;
4227
4228 /* Self must be a valid buffer */
4229 if (CheckBuffer(self))
4230 return -1;
4231
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004232 if (end == -1)
4233 end = self->buf->b_ml.ml_line_count;
4234
Bram Moolenaar19e60942011-06-19 00:27:51 +02004235 /* Sort out the slice range */
4236 size = end - start + 1;
4237
4238 if (lo < 0)
4239 lo = 0;
4240 else if (lo > size)
4241 lo = size;
4242 if (hi < 0)
4243 hi = 0;
4244 if (hi < lo)
4245 hi = lo;
4246 else if (hi > size)
4247 hi = size;
4248
4249 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004250 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004251 return -1;
4252
4253 if (new_end)
4254 *new_end = end + len_change;
4255
4256 return 0;
4257}
4258
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004259
4260 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004261RBAppend(
4262 BufferObject *self,
4263 PyObject *args,
4264 PyInt start,
4265 PyInt end,
4266 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004267{
4268 PyObject *lines;
4269 PyInt len_change;
4270 PyInt max;
4271 PyInt n;
4272
4273 if (CheckBuffer(self))
4274 return NULL;
4275
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004276 if (end == -1)
4277 end = self->buf->b_ml.ml_line_count;
4278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004279 max = n = end - start + 1;
4280
4281 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4282 return NULL;
4283
4284 if (n < 0 || n > max)
4285 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004286 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287 return NULL;
4288 }
4289
4290 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4291 return NULL;
4292
4293 if (new_end)
4294 *new_end = end + len_change;
4295
4296 Py_INCREF(Py_None);
4297 return Py_None;
4298}
4299
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004300/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301 */
4302
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004303static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004304static PySequenceMethods RangeAsSeq;
4305static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004306
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307typedef struct
4308{
4309 PyObject_HEAD
4310 BufferObject *buf;
4311 PyInt start;
4312 PyInt end;
4313} RangeObject;
4314
4315 static PyObject *
4316RangeNew(buf_T *buf, PyInt start, PyInt end)
4317{
4318 BufferObject *bufr;
4319 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004320 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 if (self == NULL)
4322 return NULL;
4323
4324 bufr = (BufferObject *)BufferNew(buf);
4325 if (bufr == NULL)
4326 {
4327 Py_DECREF(self);
4328 return NULL;
4329 }
4330 Py_INCREF(bufr);
4331
4332 self->buf = bufr;
4333 self->start = start;
4334 self->end = end;
4335
4336 return (PyObject *)(self);
4337}
4338
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004339 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004340RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004342 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004343 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004344 PyObject_GC_Del((void *)(self));
4345}
4346
4347 static int
4348RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4349{
4350 Py_VISIT(((PyObject *)(self->buf)));
4351 return 0;
4352}
4353
4354 static int
4355RangeClear(RangeObject *self)
4356{
4357 Py_CLEAR(self->buf);
4358 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004359}
4360
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004361 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004362RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004363{
4364 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004365 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004366 return -1; /* ??? */
4367
Bram Moolenaard6e39182013-05-21 18:30:34 +02004368 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004369}
4370
4371 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004372RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004374 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004375}
4376
4377 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004378RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004380 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004381}
4382
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004383static char *RangeAttrs[] = {
4384 "start", "end",
4385 NULL
4386};
4387
4388 static PyObject *
4389RangeDir(PyObject *self)
4390{
4391 return ObjectDir(self, RangeAttrs);
4392}
4393
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004394 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004395RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004396{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004397 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004398}
4399
4400 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004401RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004402{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004403 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004404 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4405 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004406 else
4407 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004408 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004409
4410 if (name == NULL)
4411 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004412
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004413 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004414 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004415 }
4416}
4417
4418static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004419 /* name, function, calling, documentation */
4420 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004421 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4422 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004423};
4424
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004425static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004426static PySequenceMethods BufferAsSeq;
4427static PyMappingMethods BufferAsMapping;
4428
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004429 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004430BufferNew(buf_T *buf)
4431{
4432 /* We need to handle deletion of buffers underneath us.
4433 * If we add a "b_python*_ref" field to the buf_T structure,
4434 * then we can get at it in buf_freeall() in vim. We then
4435 * need to create only ONE Python object per buffer - if
4436 * we try to create a second, just INCREF the existing one
4437 * and return it. The (single) Python object referring to
4438 * the buffer is stored in "b_python*_ref".
4439 * Question: what to do on a buf_freeall(). We'll probably
4440 * have to either delete the Python object (DECREF it to
4441 * zero - a bad idea, as it leaves dangling refs!) or
4442 * set the buf_T * value to an invalid value (-1?), which
4443 * means we need checks in all access functions... Bah.
4444 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004445 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004446 * b_python_ref and b_python3_ref fields respectively.
4447 */
4448
4449 BufferObject *self;
4450
4451 if (BUF_PYTHON_REF(buf) != NULL)
4452 {
4453 self = BUF_PYTHON_REF(buf);
4454 Py_INCREF(self);
4455 }
4456 else
4457 {
4458 self = PyObject_NEW(BufferObject, &BufferType);
4459 if (self == NULL)
4460 return NULL;
4461 self->buf = buf;
4462 BUF_PYTHON_REF(buf) = self;
4463 }
4464
4465 return (PyObject *)(self);
4466}
4467
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004468 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004469BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004470{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004471 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4472 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004473
4474 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004475}
4476
Bram Moolenaar971db462013-05-12 18:44:48 +02004477 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004478BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004479{
4480 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004481 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004482 return -1; /* ??? */
4483
Bram Moolenaard6e39182013-05-21 18:30:34 +02004484 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004485}
4486
4487 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004488BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004489{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004490 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004491}
4492
4493 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004494BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004495{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004496 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004497}
4498
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004499static char *BufferAttrs[] = {
4500 "name", "number", "vars", "options", "valid",
4501 NULL
4502};
4503
4504 static PyObject *
4505BufferDir(PyObject *self)
4506{
4507 return ObjectDir(self, BufferAttrs);
4508}
4509
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004510 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004511BufferAttrValid(BufferObject *self, char *name)
4512{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004513 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004514
4515 if (strcmp(name, "valid") != 0)
4516 return NULL;
4517
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004518 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4519 Py_INCREF(ret);
4520 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004521}
4522
4523 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004524BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004525{
4526 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004527 return PyString_FromString((self->buf->b_ffname == NULL
4528 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004529 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004530 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004531 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004532 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004533 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004534 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4535 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004536 else if (strcmp(name, "__members__") == 0)
4537 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004538 else
4539 return NULL;
4540}
4541
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004542 static int
4543BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4544{
4545 if (CheckBuffer(self))
4546 return -1;
4547
4548 if (strcmp(name, "name") == 0)
4549 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004550 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004551 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004552 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004553 PyObject *todecref;
4554
4555 if (!(val = StringToChars(valObject, &todecref)))
4556 return -1;
4557
4558 VimTryStart();
4559 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4560 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004561 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004562 aucmd_restbuf(&aco);
4563 Py_XDECREF(todecref);
4564 if (VimTryEnd())
4565 return -1;
4566
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004567 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004568 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004569 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004570 return -1;
4571 }
4572 return 0;
4573 }
4574 else
4575 {
4576 PyErr_SetString(PyExc_AttributeError, name);
4577 return -1;
4578 }
4579}
4580
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004581 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004582BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004583{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004584 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004585}
4586
4587 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004588BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004589{
4590 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004591 char_u *pmark;
4592 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004593 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004594 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595
Bram Moolenaard6e39182013-05-21 18:30:34 +02004596 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004597 return NULL;
4598
Bram Moolenaar389a1792013-06-23 13:00:44 +02004599 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004600 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004601
Bram Moolenaar389a1792013-06-23 13:00:44 +02004602 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004603 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004604 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004605 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004606 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004607 return NULL;
4608 }
4609
4610 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004611
4612 Py_XDECREF(todecref);
4613
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004614 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004615 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004616 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004617 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004618 if (VimTryEnd())
4619 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004620
4621 if (posp == NULL)
4622 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004623 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004624 return NULL;
4625 }
4626
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004627 if (posp->lnum <= 0)
4628 {
4629 /* Or raise an error? */
4630 Py_INCREF(Py_None);
4631 return Py_None;
4632 }
4633
4634 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4635}
4636
4637 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004638BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004639{
4640 PyInt start;
4641 PyInt end;
4642
Bram Moolenaard6e39182013-05-21 18:30:34 +02004643 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004644 return NULL;
4645
4646 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4647 return NULL;
4648
Bram Moolenaard6e39182013-05-21 18:30:34 +02004649 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004650}
4651
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004652 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004653BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004654{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004655 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004656 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004657 else
4658 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004659 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004660
4661 if (name == NULL)
4662 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004663
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004664 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004665 }
4666}
4667
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004669 /* name, function, calling, documentation */
4670 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004671 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004672 {"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 +02004673 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4674 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675};
4676
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004677/*
4678 * Buffer list object - Implementation
4679 */
4680
4681static PyTypeObject BufMapType;
4682
4683typedef struct
4684{
4685 PyObject_HEAD
4686} BufMapObject;
4687
4688 static PyInt
4689BufMapLength(PyObject *self UNUSED)
4690{
4691 buf_T *b = firstbuf;
4692 PyInt n = 0;
4693
4694 while (b)
4695 {
4696 ++n;
4697 b = b->b_next;
4698 }
4699
4700 return n;
4701}
4702
4703 static PyObject *
4704BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4705{
4706 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004707 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004708
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004709 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004710 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004711
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004712 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004713
4714 if (b)
4715 return BufferNew(b);
4716 else
4717 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004718 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004719 return NULL;
4720 }
4721}
4722
4723 static void
4724BufMapIterDestruct(PyObject *buffer)
4725{
4726 /* Iteration was stopped before all buffers were processed */
4727 if (buffer)
4728 {
4729 Py_DECREF(buffer);
4730 }
4731}
4732
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004733 static int
4734BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4735{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004736 if (buffer)
4737 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004738 return 0;
4739}
4740
4741 static int
4742BufMapIterClear(PyObject **buffer)
4743{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004744 if (*buffer)
4745 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004746 return 0;
4747}
4748
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004749 static PyObject *
4750BufMapIterNext(PyObject **buffer)
4751{
4752 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004753 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004754
4755 if (!*buffer)
4756 return NULL;
4757
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004758 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004760 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004761 {
4762 *buffer = NULL;
4763 return NULL;
4764 }
4765
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004766 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004767 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004768 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004769 return NULL;
4770 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004771 /* Do not increment reference: we no longer hold it (decref), but whoever
4772 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004773 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004774}
4775
4776 static PyObject *
4777BufMapIter(PyObject *self UNUSED)
4778{
4779 PyObject *buffer;
4780
4781 buffer = BufferNew(firstbuf);
4782 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004783 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4784 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004785}
4786
4787static PyMappingMethods BufMapAsMapping = {
4788 (lenfunc) BufMapLength,
4789 (binaryfunc) BufMapItem,
4790 (objobjargproc) 0,
4791};
4792
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004793/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004794 */
4795
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004796static char *CurrentAttrs[] = {
4797 "buffer", "window", "line", "range", "tabpage",
4798 NULL
4799};
4800
4801 static PyObject *
4802CurrentDir(PyObject *self)
4803{
4804 return ObjectDir(self, CurrentAttrs);
4805}
4806
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004807 static PyObject *
4808CurrentGetattr(PyObject *self UNUSED, char *name)
4809{
4810 if (strcmp(name, "buffer") == 0)
4811 return (PyObject *)BufferNew(curbuf);
4812 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004813 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004814 else if (strcmp(name, "tabpage") == 0)
4815 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004816 else if (strcmp(name, "line") == 0)
4817 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4818 else if (strcmp(name, "range") == 0)
4819 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004820 else if (strcmp(name, "__members__") == 0)
4821 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004822 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004823#if PY_MAJOR_VERSION < 3
4824 return Py_FindMethod(WindowMethods, self, name);
4825#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004826 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004827#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004828}
4829
4830 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004831CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004832{
4833 if (strcmp(name, "line") == 0)
4834 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004835 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4836 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004837 return -1;
4838
4839 return 0;
4840 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004841 else if (strcmp(name, "buffer") == 0)
4842 {
4843 int count;
4844
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004845 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004846 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004847 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004848 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004849 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004850 return -1;
4851 }
4852
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004853 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004854 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004855 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004856
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004857 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004858 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4859 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004860 if (VimTryEnd())
4861 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004862 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004863 return -1;
4864 }
4865
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004866 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004867 }
4868 else if (strcmp(name, "window") == 0)
4869 {
4870 int count;
4871
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004872 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004873 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004874 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004875 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004876 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004877 return -1;
4878 }
4879
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004880 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004881 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004882 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004883
4884 if (!count)
4885 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004886 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004887 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004888 return -1;
4889 }
4890
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004891 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004892 win_goto(((WindowObject *)(valObject))->win);
4893 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004894 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004895 if (VimTryEnd())
4896 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004897 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004898 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004899 return -1;
4900 }
4901
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004902 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004903 }
4904 else if (strcmp(name, "tabpage") == 0)
4905 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004906 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004907 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004908 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004909 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004910 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004911 return -1;
4912 }
4913
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004914 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004915 return -1;
4916
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004917 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004918 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4919 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004920 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004921 if (VimTryEnd())
4922 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004923 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004924 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004925 return -1;
4926 }
4927
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004928 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004929 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004930 else
4931 {
4932 PyErr_SetString(PyExc_AttributeError, name);
4933 return -1;
4934 }
4935}
4936
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004937static struct PyMethodDef CurrentMethods[] = {
4938 /* name, function, calling, documentation */
4939 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4940 { NULL, NULL, 0, NULL}
4941};
4942
Bram Moolenaardb913952012-06-29 12:54:53 +02004943 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004944init_range_cmd(exarg_T *eap)
4945{
4946 RangeStart = eap->line1;
4947 RangeEnd = eap->line2;
4948}
4949
4950 static void
4951init_range_eval(typval_T *rettv UNUSED)
4952{
4953 RangeStart = (PyInt) curwin->w_cursor.lnum;
4954 RangeEnd = RangeStart;
4955}
4956
4957 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004958run_cmd(const char *cmd, void *arg UNUSED
4959#ifdef PY_CAN_RECURSE
4960 , PyGILState_STATE *pygilstate UNUSED
4961#endif
4962 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004963{
4964 PyRun_SimpleString((char *) cmd);
4965}
4966
4967static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4968static int code_hdr_len = 30;
4969
4970 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004971run_do(const char *cmd, void *arg UNUSED
4972#ifdef PY_CAN_RECURSE
4973 , PyGILState_STATE *pygilstate
4974#endif
4975 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004976{
4977 PyInt lnum;
4978 size_t len;
4979 char *code;
4980 int status;
4981 PyObject *pyfunc, *pymain;
4982
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004983 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004984 {
4985 EMSG(_("cannot save undo information"));
4986 return;
4987 }
4988
4989 len = code_hdr_len + STRLEN(cmd);
4990 code = PyMem_New(char, len + 1);
4991 memcpy(code, code_hdr, code_hdr_len);
4992 STRCPY(code + code_hdr_len, cmd);
4993 status = PyRun_SimpleString(code);
4994 PyMem_Free(code);
4995
4996 if (status)
4997 {
4998 EMSG(_("failed to run the code"));
4999 return;
5000 }
5001
5002 status = 0;
5003 pymain = PyImport_AddModule("__main__");
5004 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005005#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005006 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005007#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005008
5009 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5010 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005011 PyObject *line;
5012 PyObject *linenr;
5013 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005014
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005015#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005016 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005017#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005018 if (!(line = GetBufferLine(curbuf, lnum)))
5019 goto err;
5020 if (!(linenr = PyInt_FromLong((long) lnum)))
5021 {
5022 Py_DECREF(line);
5023 goto err;
5024 }
5025 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5026 Py_DECREF(line);
5027 Py_DECREF(linenr);
5028 if (!ret)
5029 goto err;
5030
5031 if (ret != Py_None)
5032 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5033 goto err;
5034
5035 Py_XDECREF(ret);
5036 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005037#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005038 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005039#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005040 }
5041 goto out;
5042err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005043#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005044 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005045#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005046 PyErr_PrintEx(0);
5047 PythonIO_Flush();
5048 status = 1;
5049out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005050#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005051 if (!status)
5052 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005053#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005054 Py_DECREF(pyfunc);
5055 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5056 if (status)
5057 return;
5058 check_cursor();
5059 update_curbuf(NOT_VALID);
5060}
5061
5062 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005063run_eval(const char *cmd, typval_T *rettv
5064#ifdef PY_CAN_RECURSE
5065 , PyGILState_STATE *pygilstate UNUSED
5066#endif
5067 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005068{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005069 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005070
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005071 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5072 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005073 {
5074 if (PyErr_Occurred() && !msg_silent)
5075 PyErr_PrintEx(0);
5076 EMSG(_("E858: Eval did not return a valid python object"));
5077 }
5078 else
5079 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005080 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005081 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005082 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005083 }
5084 PyErr_Clear();
5085}
5086
5087 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005088set_ref_in_py(const int copyID)
5089{
5090 pylinkedlist_T *cur;
5091 dict_T *dd;
5092 list_T *ll;
5093
5094 if (lastdict != NULL)
5095 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5096 {
5097 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5098 if (dd->dv_copyID != copyID)
5099 {
5100 dd->dv_copyID = copyID;
5101 set_ref_in_ht(&dd->dv_hashtab, copyID);
5102 }
5103 }
5104
5105 if (lastlist != NULL)
5106 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5107 {
5108 ll = ((ListObject *) (cur->pll_obj))->list;
5109 if (ll->lv_copyID != copyID)
5110 {
5111 ll->lv_copyID = copyID;
5112 set_ref_in_list(ll, copyID);
5113 }
5114 }
5115}
5116
5117 static int
5118set_string_copy(char_u *str, typval_T *tv)
5119{
5120 tv->vval.v_string = vim_strsave(str);
5121 if (tv->vval.v_string == NULL)
5122 {
5123 PyErr_NoMemory();
5124 return -1;
5125 }
5126 return 0;
5127}
5128
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005130pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005132 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133 char_u *key;
5134 dictitem_T *di;
5135 PyObject *keyObject;
5136 PyObject *valObject;
5137 Py_ssize_t iter = 0;
5138
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005139 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005141
5142 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005143 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005144
5145 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5146 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005147 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005148
Bram Moolenaara03e6312013-05-29 22:49:26 +02005149 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005150 {
5151 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005152 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005153 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005154
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005155 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005156 {
5157 dict_unref(dict);
5158 return -1;
5159 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005160
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005161 if (*key == NUL)
5162 {
5163 dict_unref(dict);
5164 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005165 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005166 return -1;
5167 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005168
5169 di = dictitem_alloc(key);
5170
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005171 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172
5173 if (di == NULL)
5174 {
5175 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005176 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005177 return -1;
5178 }
5179 di->di_tv.v_lock = 0;
5180
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005181 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005182 {
5183 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005184 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005185 return -1;
5186 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005187
5188 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005189 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005190 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005191 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005192 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005193 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005194 return -1;
5195 }
5196 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005197
5198 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005199 return 0;
5200}
5201
5202 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005203pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005204{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005205 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005206 char_u *key;
5207 dictitem_T *di;
5208 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005209 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005210 PyObject *keyObject;
5211 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005212
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005213 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005214 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005215
5216 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005217 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005218
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005219 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005220 {
5221 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005223 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005224
5225 if (!(iterator = PyObject_GetIter(list)))
5226 {
5227 dict_unref(dict);
5228 Py_DECREF(list);
5229 return -1;
5230 }
5231 Py_DECREF(list);
5232
5233 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005234 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005235 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005236
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005237 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005238 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005239 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005240 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005241 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005242 return -1;
5243 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005244
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005245 if (*key == NUL)
5246 {
5247 Py_DECREF(keyObject);
5248 Py_DECREF(iterator);
5249 Py_XDECREF(todecref);
5250 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005251 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005252 return -1;
5253 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005255 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005257 Py_DECREF(keyObject);
5258 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005259 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005260 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005261 return -1;
5262 }
5263
5264 di = dictitem_alloc(key);
5265
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005266 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005267 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005268
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 if (di == NULL)
5270 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005271 Py_DECREF(iterator);
5272 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005273 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 PyErr_NoMemory();
5275 return -1;
5276 }
5277 di->di_tv.v_lock = 0;
5278
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005279 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005280 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005281 Py_DECREF(iterator);
5282 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005283 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005284 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005285 return -1;
5286 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005287
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005288 Py_DECREF(valObject);
5289
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005290 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005291 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005292 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005293 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005294 dictitem_free(di);
5295 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005296 return -1;
5297 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005298 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005299 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005300 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005301 return 0;
5302}
5303
5304 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005305pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005306{
5307 list_T *l;
5308
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005309 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005310 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005311
5312 tv->v_type = VAR_LIST;
5313 tv->vval.v_list = l;
5314
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005315 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005316 {
5317 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005318 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005319 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005320
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005321 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005322 return 0;
5323}
5324
Bram Moolenaardb913952012-06-29 12:54:53 +02005325typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5326
5327 static int
5328convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005329 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005330{
5331 PyObject *capsule;
5332 char hexBuf[sizeof(void *) * 2 + 3];
5333
5334 sprintf(hexBuf, "%p", obj);
5335
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005336# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005337 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005338# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005339 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005340# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005341 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005342 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005343# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005344 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005345# else
5346 capsule = PyCObject_FromVoidPtr(tv, NULL);
5347# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005348 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5349 {
5350 Py_DECREF(capsule);
5351 tv->v_type = VAR_UNKNOWN;
5352 return -1;
5353 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005354
5355 Py_DECREF(capsule);
5356
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005357 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005358 {
5359 tv->v_type = VAR_UNKNOWN;
5360 return -1;
5361 }
5362 /* As we are not using copy_tv which increments reference count we must
5363 * do it ourself. */
5364 switch(tv->v_type)
5365 {
5366 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5367 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5368 }
5369 }
5370 else
5371 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005372 typval_T *v;
5373
5374# ifdef PY_USE_CAPSULE
5375 v = PyCapsule_GetPointer(capsule, NULL);
5376# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005377 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005378# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005379 copy_tv(v, tv);
5380 }
5381 return 0;
5382}
5383
5384 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005385ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5386{
5387 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005388 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005389
5390 if (!(lookup_dict = PyDict_New()))
5391 return -1;
5392
5393 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5394 {
5395 tv->v_type = VAR_DICT;
5396 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5397 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005398 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005399 }
5400 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005401 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005402 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005403 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005404 else
5405 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005406 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005407 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005408 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005409 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005410 }
5411 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005412 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005413}
5414
5415 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005416ConvertFromPyObject(PyObject *obj, typval_T *tv)
5417{
5418 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005419 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005420
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005421 if (!(lookup_dict = PyDict_New()))
5422 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005423 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005424 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005425 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005426}
5427
5428 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005429_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005430{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005431 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005432 {
5433 tv->v_type = VAR_DICT;
5434 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5435 ++tv->vval.v_dict->dv_refcount;
5436 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005437 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005438 {
5439 tv->v_type = VAR_LIST;
5440 tv->vval.v_list = (((ListObject *)(obj))->list);
5441 ++tv->vval.v_list->lv_refcount;
5442 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005443 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005444 {
5445 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5446 return -1;
5447
5448 tv->v_type = VAR_FUNC;
5449 func_ref(tv->vval.v_string);
5450 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005451 else if (PyBytes_Check(obj))
5452 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005454
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005455 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005456 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005457 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005458 return -1;
5459
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005460 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005461 return -1;
5462
5463 tv->v_type = VAR_STRING;
5464 }
5465 else if (PyUnicode_Check(obj))
5466 {
5467 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005469
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005470 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005471 if (bytes == NULL)
5472 return -1;
5473
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005475 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005476 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005477 return -1;
5478
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005479 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005480 {
5481 Py_XDECREF(bytes);
5482 return -1;
5483 }
5484 Py_XDECREF(bytes);
5485
5486 tv->v_type = VAR_STRING;
5487 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005488#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005489 else if (PyInt_Check(obj))
5490 {
5491 tv->v_type = VAR_NUMBER;
5492 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005493 if (PyErr_Occurred())
5494 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005495 }
5496#endif
5497 else if (PyLong_Check(obj))
5498 {
5499 tv->v_type = VAR_NUMBER;
5500 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005501 if (PyErr_Occurred())
5502 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005503 }
5504 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005505 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005506#ifdef FEAT_FLOAT
5507 else if (PyFloat_Check(obj))
5508 {
5509 tv->v_type = VAR_FLOAT;
5510 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5511 }
5512#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005513 else if (PyObject_HasAttrString(obj, "keys"))
5514 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005515 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005516 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005517 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005518 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005519 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005520 else if (PyNumber_Check(obj))
5521 {
5522 PyObject *num;
5523
5524 if (!(num = PyNumber_Long(obj)))
5525 return -1;
5526
5527 tv->v_type = VAR_NUMBER;
5528 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5529
5530 Py_DECREF(num);
5531 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005532 else
5533 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005534 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005535 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005536 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005537 return -1;
5538 }
5539 return 0;
5540}
5541
5542 static PyObject *
5543ConvertToPyObject(typval_T *tv)
5544{
5545 if (tv == NULL)
5546 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005547 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005548 return NULL;
5549 }
5550 switch (tv->v_type)
5551 {
5552 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005553 return PyBytes_FromString(tv->vval.v_string == NULL
5554 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005555 case VAR_NUMBER:
5556 return PyLong_FromLong((long) tv->vval.v_number);
5557#ifdef FEAT_FLOAT
5558 case VAR_FLOAT:
5559 return PyFloat_FromDouble((double) tv->vval.v_float);
5560#endif
5561 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005562 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005563 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005564 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005565 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005566 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005567 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005568 case VAR_UNKNOWN:
5569 Py_INCREF(Py_None);
5570 return Py_None;
5571 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005572 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005573 return NULL;
5574 }
5575}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005576
5577typedef struct
5578{
5579 PyObject_HEAD
5580} CurrentObject;
5581static PyTypeObject CurrentType;
5582
5583 static void
5584init_structs(void)
5585{
5586 vim_memset(&OutputType, 0, sizeof(OutputType));
5587 OutputType.tp_name = "vim.message";
5588 OutputType.tp_basicsize = sizeof(OutputObject);
5589 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5590 OutputType.tp_doc = "vim message object";
5591 OutputType.tp_methods = OutputMethods;
5592#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005593 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5594 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005595 OutputType.tp_alloc = call_PyType_GenericAlloc;
5596 OutputType.tp_new = call_PyType_GenericNew;
5597 OutputType.tp_free = call_PyObject_Free;
5598#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005599 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5600 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005601#endif
5602
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005603 vim_memset(&IterType, 0, sizeof(IterType));
5604 IterType.tp_name = "vim.iter";
5605 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005606 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005607 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005608 IterType.tp_iter = (getiterfunc)IterIter;
5609 IterType.tp_iternext = (iternextfunc)IterNext;
5610 IterType.tp_dealloc = (destructor)IterDestructor;
5611 IterType.tp_traverse = (traverseproc)IterTraverse;
5612 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005613
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005614 vim_memset(&BufferType, 0, sizeof(BufferType));
5615 BufferType.tp_name = "vim.buffer";
5616 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005617 BufferType.tp_dealloc = (destructor)BufferDestructor;
5618 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005619 BufferType.tp_as_sequence = &BufferAsSeq;
5620 BufferType.tp_as_mapping = &BufferAsMapping;
5621 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5622 BufferType.tp_doc = "vim buffer object";
5623 BufferType.tp_methods = BufferMethods;
5624#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005625 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005626 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005627 BufferType.tp_alloc = call_PyType_GenericAlloc;
5628 BufferType.tp_new = call_PyType_GenericNew;
5629 BufferType.tp_free = call_PyObject_Free;
5630#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005631 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005632 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005633#endif
5634
5635 vim_memset(&WindowType, 0, sizeof(WindowType));
5636 WindowType.tp_name = "vim.window";
5637 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005638 WindowType.tp_dealloc = (destructor)WindowDestructor;
5639 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005640 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005641 WindowType.tp_doc = "vim Window object";
5642 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005643 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5644 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005645#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005646 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5647 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005648 WindowType.tp_alloc = call_PyType_GenericAlloc;
5649 WindowType.tp_new = call_PyType_GenericNew;
5650 WindowType.tp_free = call_PyObject_Free;
5651#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005652 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5653 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005654#endif
5655
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005656 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5657 TabPageType.tp_name = "vim.tabpage";
5658 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005659 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5660 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005661 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5662 TabPageType.tp_doc = "vim tab page object";
5663 TabPageType.tp_methods = TabPageMethods;
5664#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005665 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005666 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5667 TabPageType.tp_new = call_PyType_GenericNew;
5668 TabPageType.tp_free = call_PyObject_Free;
5669#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005670 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005671#endif
5672
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005673 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5674 BufMapType.tp_name = "vim.bufferlist";
5675 BufMapType.tp_basicsize = sizeof(BufMapObject);
5676 BufMapType.tp_as_mapping = &BufMapAsMapping;
5677 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005678 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005679 BufferType.tp_doc = "vim buffer list";
5680
5681 vim_memset(&WinListType, 0, sizeof(WinListType));
5682 WinListType.tp_name = "vim.windowlist";
5683 WinListType.tp_basicsize = sizeof(WinListType);
5684 WinListType.tp_as_sequence = &WinListAsSeq;
5685 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5686 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005687 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005688
5689 vim_memset(&TabListType, 0, sizeof(TabListType));
5690 TabListType.tp_name = "vim.tabpagelist";
5691 TabListType.tp_basicsize = sizeof(TabListType);
5692 TabListType.tp_as_sequence = &TabListAsSeq;
5693 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5694 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005695
5696 vim_memset(&RangeType, 0, sizeof(RangeType));
5697 RangeType.tp_name = "vim.range";
5698 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005699 RangeType.tp_dealloc = (destructor)RangeDestructor;
5700 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005701 RangeType.tp_as_sequence = &RangeAsSeq;
5702 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005703 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005704 RangeType.tp_doc = "vim Range object";
5705 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005706 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5707 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005708#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005709 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005710 RangeType.tp_alloc = call_PyType_GenericAlloc;
5711 RangeType.tp_new = call_PyType_GenericNew;
5712 RangeType.tp_free = call_PyObject_Free;
5713#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005714 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005715#endif
5716
5717 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5718 CurrentType.tp_name = "vim.currentdata";
5719 CurrentType.tp_basicsize = sizeof(CurrentObject);
5720 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5721 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005722 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005723#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005724 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5725 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005726#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005727 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5728 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005729#endif
5730
5731 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5732 DictionaryType.tp_name = "vim.dictionary";
5733 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005734 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005735 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005736 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005737 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005738 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5739 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005740 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5741 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5742 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005743#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005744 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5745 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005746#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005747 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5748 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005749#endif
5750
5751 vim_memset(&ListType, 0, sizeof(ListType));
5752 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005753 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005754 ListType.tp_basicsize = sizeof(ListObject);
5755 ListType.tp_as_sequence = &ListAsSeq;
5756 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005757 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005758 ListType.tp_doc = "list pushing modifications to vim structure";
5759 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005760 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005761 ListType.tp_new = (newfunc)ListConstructor;
5762 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005763#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005764 ListType.tp_getattro = (getattrofunc)ListGetattro;
5765 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005766#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005767 ListType.tp_getattr = (getattrfunc)ListGetattr;
5768 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005769#endif
5770
5771 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005772 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005773 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005774 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5775 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005776 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005777 FunctionType.tp_doc = "object that calls vim function";
5778 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005779 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005780 FunctionType.tp_new = (newfunc)FunctionConstructor;
5781 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005782#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005783 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005784#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005785 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005786#endif
5787
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005788 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5789 OptionsType.tp_name = "vim.options";
5790 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005791 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005792 OptionsType.tp_doc = "object for manipulating options";
5793 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005794 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5795 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5796 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005797
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005798 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5799 LoaderType.tp_name = "vim.Loader";
5800 LoaderType.tp_basicsize = sizeof(LoaderObject);
5801 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5802 LoaderType.tp_doc = "vim message object";
5803 LoaderType.tp_methods = LoaderMethods;
5804 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5805
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005806#if PY_MAJOR_VERSION >= 3
5807 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5808 vimmodule.m_name = "vim";
5809 vimmodule.m_doc = "Vim Python interface\n";
5810 vimmodule.m_size = -1;
5811 vimmodule.m_methods = VimMethods;
5812#endif
5813}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005814
5815#define PYTYPE_READY(type) \
5816 if (PyType_Ready(&type)) \
5817 return -1;
5818
5819 static int
5820init_types()
5821{
5822 PYTYPE_READY(IterType);
5823 PYTYPE_READY(BufferType);
5824 PYTYPE_READY(RangeType);
5825 PYTYPE_READY(WindowType);
5826 PYTYPE_READY(TabPageType);
5827 PYTYPE_READY(BufMapType);
5828 PYTYPE_READY(WinListType);
5829 PYTYPE_READY(TabListType);
5830 PYTYPE_READY(CurrentType);
5831 PYTYPE_READY(DictionaryType);
5832 PYTYPE_READY(ListType);
5833 PYTYPE_READY(FunctionType);
5834 PYTYPE_READY(OptionsType);
5835 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005836 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005837 return 0;
5838}
5839
5840 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005841init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005842{
5843 PyObject *path;
5844 PyObject *path_hook;
5845 PyObject *path_hooks;
5846
5847 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5848 return -1;
5849
5850 if (!(path_hooks = PySys_GetObject("path_hooks")))
5851 {
5852 PyErr_Clear();
5853 path_hooks = PyList_New(1);
5854 PyList_SET_ITEM(path_hooks, 0, path_hook);
5855 if (PySys_SetObject("path_hooks", path_hooks))
5856 {
5857 Py_DECREF(path_hooks);
5858 return -1;
5859 }
5860 Py_DECREF(path_hooks);
5861 }
5862 else if (PyList_Check(path_hooks))
5863 {
5864 if (PyList_Append(path_hooks, path_hook))
5865 {
5866 Py_DECREF(path_hook);
5867 return -1;
5868 }
5869 Py_DECREF(path_hook);
5870 }
5871 else
5872 {
5873 VimTryStart();
5874 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5875 "You should now do the following:\n"
5876 "- append vim.path_hook to sys.path_hooks\n"
5877 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5878 VimTryEnd(); /* Discard the error */
5879 Py_DECREF(path_hook);
5880 return 0;
5881 }
5882
5883 if (!(path = PySys_GetObject("path")))
5884 {
5885 PyErr_Clear();
5886 path = PyList_New(1);
5887 Py_INCREF(vim_special_path_object);
5888 PyList_SET_ITEM(path, 0, vim_special_path_object);
5889 if (PySys_SetObject("path", path))
5890 {
5891 Py_DECREF(path);
5892 return -1;
5893 }
5894 Py_DECREF(path);
5895 }
5896 else if (PyList_Check(path))
5897 {
5898 if (PyList_Append(path, vim_special_path_object))
5899 return -1;
5900 }
5901 else
5902 {
5903 VimTryStart();
5904 EMSG(_("Failed to set path: sys.path is not a list\n"
5905 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5906 VimTryEnd(); /* Discard the error */
5907 }
5908
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005909 return 0;
5910}
5911
5912static BufMapObject TheBufferMap =
5913{
5914 PyObject_HEAD_INIT(&BufMapType)
5915};
5916
5917static WinListObject TheWindowList =
5918{
5919 PyObject_HEAD_INIT(&WinListType)
5920 NULL
5921};
5922
5923static CurrentObject TheCurrent =
5924{
5925 PyObject_HEAD_INIT(&CurrentType)
5926};
5927
5928static TabListObject TheTabPageList =
5929{
5930 PyObject_HEAD_INIT(&TabListType)
5931};
5932
5933static struct numeric_constant {
5934 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005935 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005936} numeric_constants[] = {
5937 {"VAR_LOCKED", VAR_LOCKED},
5938 {"VAR_FIXED", VAR_FIXED},
5939 {"VAR_SCOPE", VAR_SCOPE},
5940 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5941};
5942
5943static struct object_constant {
5944 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005945 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005946} object_constants[] = {
5947 {"buffers", (PyObject *)(void *)&TheBufferMap},
5948 {"windows", (PyObject *)(void *)&TheWindowList},
5949 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5950 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005951
5952 {"Buffer", (PyObject *)&BufferType},
5953 {"Range", (PyObject *)&RangeType},
5954 {"Window", (PyObject *)&WindowType},
5955 {"TabPage", (PyObject *)&TabPageType},
5956 {"Dictionary", (PyObject *)&DictionaryType},
5957 {"List", (PyObject *)&ListType},
5958 {"Function", (PyObject *)&FunctionType},
5959 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005960 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005961};
5962
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005963#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02005964 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005965 return -1;
5966
5967#define ADD_CHECKED_OBJECT(m, name, obj) \
5968 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005969 PyObject *valObject = obj; \
5970 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005971 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005972 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005973 }
5974
5975 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02005976populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005977{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005978 int i;
5979 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005980 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005981 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005982
5983 for (i = 0; i < (int)(sizeof(numeric_constants)
5984 / sizeof(struct numeric_constant));
5985 ++i)
5986 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005987 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005988
5989 for (i = 0; i < (int)(sizeof(object_constants)
5990 / sizeof(struct object_constant));
5991 ++i)
5992 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005993 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005994
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005995 valObject = object_constants[i].valObject;
5996 Py_INCREF(valObject);
5997 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005998 }
5999
6000 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6001 return -1;
6002 ADD_OBJECT(m, "error", VimError);
6003
Bram Moolenaara9922d62013-05-30 13:01:18 +02006004 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6005 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006006 ADD_CHECKED_OBJECT(m, "options",
6007 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006008
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006009 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006010 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006011 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006012
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006013 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006014 return -1;
6015 ADD_OBJECT(m, "_getcwd", py_getcwd)
6016
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006017 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006018 return -1;
6019 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006020 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006021 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006022 if (PyObject_SetAttrString(other_module, "chdir", attr))
6023 {
6024 Py_DECREF(attr);
6025 return -1;
6026 }
6027 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006028
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006029 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006030 {
6031 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006032 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006033 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006034 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6035 {
6036 Py_DECREF(attr);
6037 return -1;
6038 }
6039 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006040 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006041 else
6042 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006043
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006044 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6045 return -1;
6046
6047 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6048
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006049 if (!(imp = PyImport_ImportModule("imp")))
6050 return -1;
6051
6052 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6053 {
6054 Py_DECREF(imp);
6055 return -1;
6056 }
6057
6058 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6059 {
6060 Py_DECREF(py_find_module);
6061 Py_DECREF(imp);
6062 return -1;
6063 }
6064
6065 Py_DECREF(imp);
6066
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006067 ADD_OBJECT(m, "_find_module", py_find_module);
6068 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006069
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006070 return 0;
6071}