blob: e669d84099a16ab5b85a31b3623b291389a14e1b [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
21# define ENC_OPT p_enc
22#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 Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
28
29#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
30#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020031#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032
Bram Moolenaare9ba5162013-05-29 22:02:22 +020033#define DICTKEY_DECL \
Bram Moolenaara9922d62013-05-30 13:01:18 +020034 PyObject *dictkey_todecref = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020035#define DICTKEY_CHECK_EMPTY(err) \
36 if (*key == NUL) \
37 { \
38 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
39 return err; \
40 }
41#define DICTKEY_SET_KEY (key = StringToChars(keyObject, &dictkey_todecref))
Bram Moolenaara03e6312013-05-29 22:49:26 +020042#define DICTKEY_GET(err, decref) \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020043 if (!DICTKEY_SET_KEY) \
Bram Moolenaara03e6312013-05-29 22:49:26 +020044 { \
45 if (decref) \
46 { \
47 Py_DECREF(keyObject); \
48 } \
Bram Moolenaarc37b6ec2013-05-29 22:43:37 +020049 return err; \
Bram Moolenaara03e6312013-05-29 22:49:26 +020050 } \
51 if (decref && !dictkey_todecref) \
52 dictkey_todecref = keyObject; \
Bram Moolenaar9bb77d62013-05-30 12:14:49 +020053 DICTKEY_CHECK_EMPTY(err)
Bram Moolenaare9ba5162013-05-29 22:02:22 +020054#define DICTKEY_UNREF \
55 Py_XDECREF(dictkey_todecref);
56
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 Moolenaar170bf1a2010-07-24 23:51:45 +020076/*
77 * obtain a lock on the Vim data structures
78 */
79 static void
80Python_Lock_Vim(void)
81{
82}
83
84/*
85 * release a lock on the Vim data structures
86 */
87 static void
88Python_Release_Vim(void)
89{
90}
91
Bram Moolenaare9ba5162013-05-29 22:02:22 +020092/*
93 * The "todecref" argument holds a pointer to PyObject * that must be
94 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
95 * was needed to generate returned value is object.
96 *
97 * Use Py_XDECREF to decrement reference count.
98 */
99 static char_u *
100StringToChars(PyObject *object, PyObject **todecref)
101{
102 char_u *p;
103 PyObject *bytes = NULL;
104
105 if (PyBytes_Check(object))
106 {
107
108 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
109 return NULL;
110 if (p == NULL)
111 return NULL;
112
113 *todecref = NULL;
114 }
115 else if (PyUnicode_Check(object))
116 {
117 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
118 if (bytes == NULL)
119 return NULL;
120
121 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
122 return NULL;
123 if (p == NULL)
124 return NULL;
125
126 *todecref = bytes;
127 }
128 else
129 {
130 PyErr_SetString(PyExc_TypeError, _("object must be string"));
131 return NULL;
132 }
133
134 return (char_u *) p;
135}
136
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200137 static int
138add_string(PyObject *list, char *s)
139{
140 PyObject *string;
141
142 if (!(string = PyString_FromString(s)))
143 return -1;
144 if (PyList_Append(list, string))
145 {
146 Py_DECREF(string);
147 return -1;
148 }
149
150 Py_DECREF(string);
151 return 0;
152}
153
154 static PyObject *
155ObjectDir(PyObject *self, char **attributes)
156{
157 PyMethodDef *method;
158 char **attr;
159 PyObject *r;
160
161 if (!(r = PyList_New(0)))
162 return NULL;
163
164 if (self)
165 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
166 if (add_string(r, (char *) method->ml_name))
167 {
168 Py_DECREF(r);
169 return NULL;
170 }
171
172 for (attr = attributes ; *attr ; ++attr)
173 if (add_string(r, *attr))
174 {
175 Py_DECREF(r);
176 return NULL;
177 }
178
179#if PY_MAJOR_VERSION < 3
180 if (add_string(r, "__members__"))
181 {
182 Py_DECREF(r);
183 return NULL;
184 }
185#endif
186
187 return r;
188}
189
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200190/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200191 */
192
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200193/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200194typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200195
196static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200197
198typedef struct
199{
200 PyObject_HEAD
201 long softspace;
202 long error;
203} OutputObject;
204
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200205static char *OutputAttrs[] = {
206 "softspace",
207 NULL
208};
209
210 static PyObject *
211OutputDir(PyObject *self)
212{
213 return ObjectDir(self, OutputAttrs);
214}
215
Bram Moolenaar77045652012-09-21 13:46:06 +0200216 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200217OutputSetattr(OutputObject *self, char *name, PyObject *val)
Bram Moolenaar77045652012-09-21 13:46:06 +0200218{
219 if (val == NULL)
220 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200221 PyErr_SetString(PyExc_AttributeError,
222 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200223 return -1;
224 }
225
226 if (strcmp(name, "softspace") == 0)
227 {
228 if (!PyInt_Check(val))
229 {
230 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
231 return -1;
232 }
233
Bram Moolenaard6e39182013-05-21 18:30:34 +0200234 self->softspace = PyInt_AsLong(val);
Bram Moolenaar77045652012-09-21 13:46:06 +0200235 return 0;
236 }
237
238 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
239 return -1;
240}
241
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200242/* Buffer IO, we write one whole line at a time. */
243static garray_T io_ga = {0, 0, 1, 80, NULL};
244static writefn old_fn = NULL;
245
246 static void
247PythonIO_Flush(void)
248{
249 if (old_fn != NULL && io_ga.ga_len > 0)
250 {
251 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
252 old_fn((char_u *)io_ga.ga_data);
253 }
254 io_ga.ga_len = 0;
255}
256
257 static void
258writer(writefn fn, char_u *str, PyInt n)
259{
260 char_u *ptr;
261
262 /* Flush when switching output function. */
263 if (fn != old_fn)
264 PythonIO_Flush();
265 old_fn = fn;
266
267 /* Write each NL separated line. Text after the last NL is kept for
268 * writing later. */
269 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
270 {
271 PyInt len = ptr - str;
272
273 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
274 break;
275
276 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
277 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
278 fn((char_u *)io_ga.ga_data);
279 str = ptr + 1;
280 n -= len + 1;
281 io_ga.ga_len = 0;
282 }
283
284 /* Put the remaining text into io_ga for later printing. */
285 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
286 {
287 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
288 io_ga.ga_len += (int)n;
289 }
290}
291
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200292 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200293OutputWrite(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200294{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200295 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200296 char *str = NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200297 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200298
Bram Moolenaar27564802011-09-07 19:30:21 +0200299 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200300 return NULL;
301
302 Py_BEGIN_ALLOW_THREADS
303 Python_Lock_Vim();
304 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
305 Python_Release_Vim();
306 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200307 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
309 Py_INCREF(Py_None);
310 return Py_None;
311}
312
313 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200314OutputWritelines(OutputObject *self, PyObject *args)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200316 PyObject *seq;
317 PyObject *iterator;
318 PyObject *item;
Bram Moolenaard6e39182013-05-21 18:30:34 +0200319 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200320
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200321 if (!PyArg_ParseTuple(args, "O", &seq))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200323
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200324 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200325 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200326
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200327 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200328 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200329 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200330 PyInt len;
331
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200332 if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
Bram Moolenaardb913952012-06-29 12:54:53 +0200333 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200334 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200335 Py_DECREF(iterator);
336 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200337 return NULL;
338 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200339 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200340
341 Py_BEGIN_ALLOW_THREADS
342 Python_Lock_Vim();
343 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
344 Python_Release_Vim();
345 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200346 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200347 }
348
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200349 Py_DECREF(iterator);
350
351 /* Iterator may have finished due to an exception */
352 if (PyErr_Occurred())
353 return NULL;
354
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200355 Py_INCREF(Py_None);
356 return Py_None;
357}
358
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100359 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200360OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100361{
362 /* do nothing */
363 Py_INCREF(Py_None);
364 return Py_None;
365}
366
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200367/***************/
368
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200369static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200370 /* name, function, calling, doc */
371 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
372 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
373 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200374 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200375 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200376};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200377
378static OutputObject Output =
379{
380 PyObject_HEAD_INIT(&OutputType)
381 0,
382 0
383};
384
385static OutputObject Error =
386{
387 PyObject_HEAD_INIT(&OutputType)
388 0,
389 1
390};
391
392 static int
393PythonIO_Init_io(void)
394{
395 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
396 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
397
398 if (PyErr_Occurred())
399 {
400 EMSG(_("E264: Python: Error initialising I/O objects"));
401 return -1;
402 }
403
404 return 0;
405}
406
407
408static PyObject *VimError;
409
410/* Check to see whether a Vim error has been reported, or a keyboard
411 * interrupt has been detected.
412 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200413
414 static void
415VimTryStart(void)
416{
417 ++trylevel;
418}
419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200420 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200421VimTryEnd(void)
422{
423 --trylevel;
424 if (got_int)
425 {
426 PyErr_SetNone(PyExc_KeyboardInterrupt);
427 return 1;
428 }
429 else if (!did_throw)
430 return 0;
431 else if (PyErr_Occurred())
432 return 1;
433 else
434 {
435 PyErr_SetVim((char *) current_exception->value);
436 discard_current_exception();
437 return 1;
438 }
439}
440
441 static int
442VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443{
444 if (got_int)
445 {
446 PyErr_SetNone(PyExc_KeyboardInterrupt);
447 return 1;
448 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 return 0;
450}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200451
452/* Vim module - Implementation
453 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200454
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200455 static PyObject *
456VimCommand(PyObject *self UNUSED, PyObject *args)
457{
458 char *cmd;
459 PyObject *result;
460
461 if (!PyArg_ParseTuple(args, "s", &cmd))
462 return NULL;
463
464 PyErr_Clear();
465
466 Py_BEGIN_ALLOW_THREADS
467 Python_Lock_Vim();
468
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200469 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200470 do_cmdline_cmd((char_u *)cmd);
471 update_screen(VALID);
472
473 Python_Release_Vim();
474 Py_END_ALLOW_THREADS
475
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200476 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200477 result = NULL;
478 else
479 result = Py_None;
480
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200481
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200482 Py_XINCREF(result);
483 return result;
484}
485
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200486/*
487 * Function to translate a typval_T into a PyObject; this will recursively
488 * translate lists/dictionaries into their Python equivalents.
489 *
490 * The depth parameter is to avoid infinite recursion, set it to 1 when
491 * you call VimToPython.
492 */
493 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200494VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200495{
496 PyObject *result;
497 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200498 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200499
500 /* Avoid infinite recursion */
501 if (depth > 100)
502 {
503 Py_INCREF(Py_None);
504 result = Py_None;
505 return result;
506 }
507
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200508 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200509 * then and we can use it again. */
510 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
511 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
512 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200513 sprintf(ptrBuf, "%p",
514 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
515 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200516
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200517 if ((result = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200518 {
519 Py_INCREF(result);
520 return result;
521 }
522 }
523
524 if (our_tv->v_type == VAR_STRING)
525 {
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200526 result = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200527 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200528 }
529 else if (our_tv->v_type == VAR_NUMBER)
530 {
531 char buf[NUMBUFLEN];
532
533 /* For backwards compatibility numbers are stored as strings. */
534 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200535 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200536 }
537# ifdef FEAT_FLOAT
538 else if (our_tv->v_type == VAR_FLOAT)
539 {
540 char buf[NUMBUFLEN];
541
542 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar432b09c2013-05-29 22:26:18 +0200543 result = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200544 }
545# endif
546 else if (our_tv->v_type == VAR_LIST)
547 {
548 list_T *list = our_tv->vval.v_list;
549 listitem_T *curr;
550
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200551 if (list == NULL)
552 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200553
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200554 if (!(result = PyList_New(0)))
555 return NULL;
556
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200557 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200558 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200559 Py_DECREF(result);
560 return NULL;
561 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200562
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200563 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
564 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200565 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200567 Py_DECREF(result);
568 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200569 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200570 if (PyList_Append(result, newObj))
571 {
572 Py_DECREF(newObj);
573 Py_DECREF(result);
574 return NULL;
575 }
576 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200577 }
578 }
579 else if (our_tv->v_type == VAR_DICT)
580 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200581
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200582 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
583 long_u todo = ht->ht_used;
584 hashitem_T *hi;
585 dictitem_T *di;
586 if (our_tv->vval.v_dict == NULL)
587 return NULL;
588
589 if (!(result = PyDict_New()))
590 return NULL;
591
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200592 if (PyDict_SetItemString(lookup_dict, ptrBuf, result))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200594 Py_DECREF(result);
595 return NULL;
596 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200598 for (hi = ht->ht_array; todo > 0; ++hi)
599 {
600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200602 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200603
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200604 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200605 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200606 {
607 Py_DECREF(result);
608 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609 }
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200610 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj))
611 {
612 Py_DECREF(result);
613 Py_DECREF(newObj);
614 return NULL;
615 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200616 }
617 }
618 }
619 else
620 {
621 Py_INCREF(Py_None);
622 result = Py_None;
623 }
624
625 return result;
626}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627
628 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200629VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200630{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 char *expr;
632 typval_T *our_tv;
633 PyObject *result;
634 PyObject *lookup_dict;
635
636 if (!PyArg_ParseTuple(args, "s", &expr))
637 return NULL;
638
639 Py_BEGIN_ALLOW_THREADS
640 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200641 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
647 return NULL;
648
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 if (our_tv == NULL)
650 {
651 PyErr_SetVim(_("invalid expression"));
652 return NULL;
653 }
654
655 /* Convert the Vim type into a Python type. Create a dictionary that's
656 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200657 if (!(lookup_dict = PyDict_New()))
658 result = NULL;
659 else
660 {
661 result = VimToPython(our_tv, 1, lookup_dict);
662 Py_DECREF(lookup_dict);
663 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664
665
666 Py_BEGIN_ALLOW_THREADS
667 Python_Lock_Vim();
668 free_tv(our_tv);
669 Python_Release_Vim();
670 Py_END_ALLOW_THREADS
671
672 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673}
674
Bram Moolenaardb913952012-06-29 12:54:53 +0200675static PyObject *ConvertToPyObject(typval_T *);
676
677 static PyObject *
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200678VimEvalPy(PyObject *self UNUSED, PyObject *args)
Bram Moolenaardb913952012-06-29 12:54:53 +0200679{
Bram Moolenaardb913952012-06-29 12:54:53 +0200680 char *expr;
681 typval_T *our_tv;
682 PyObject *result;
683
684 if (!PyArg_ParseTuple(args, "s", &expr))
685 return NULL;
686
687 Py_BEGIN_ALLOW_THREADS
688 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200689 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +0200690 our_tv = eval_expr((char_u *)expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691 Python_Release_Vim();
692 Py_END_ALLOW_THREADS
693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200694 if (VimTryEnd())
695 return NULL;
696
Bram Moolenaardb913952012-06-29 12:54:53 +0200697 if (our_tv == NULL)
698 {
699 PyErr_SetVim(_("invalid expression"));
700 return NULL;
701 }
702
703 result = ConvertToPyObject(our_tv);
704 Py_BEGIN_ALLOW_THREADS
705 Python_Lock_Vim();
706 free_tv(our_tv);
707 Python_Release_Vim();
708 Py_END_ALLOW_THREADS
709
710 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200711}
712
713 static PyObject *
714VimStrwidth(PyObject *self UNUSED, PyObject *args)
715{
716 char *expr;
717
718 if (!PyArg_ParseTuple(args, "s", &expr))
719 return NULL;
720
Bram Moolenaara54bf402012-12-05 16:30:07 +0100721 return PyLong_FromLong(
722#ifdef FEAT_MBYTE
723 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
724#else
725 STRLEN(expr)
726#endif
727 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200728}
729
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730/*
731 * Vim module - Definitions
732 */
733
734static struct PyMethodDef VimMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200735 /* name, function, calling, documentation */
736 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
737 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
738 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
739 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
740 { NULL, NULL, 0, NULL }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200741};
742
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200743/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200744 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 */
746
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200747static PyTypeObject IterType;
748
749typedef PyObject *(*nextfun)(void **);
750typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200751typedef int (*traversefun)(void *, visitproc, void *);
752typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200753
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200754/* Main purpose of this object is removing the need for do python
755 * initialization (i.e. PyType_Ready and setting type attributes) for a big
756 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200757
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200758typedef struct
759{
760 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200761 void *cur;
762 nextfun next;
763 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200764 traversefun traverse;
765 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200766} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200767
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200768 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200769IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
770 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200772 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773
Bram Moolenaar774267b2013-05-21 20:51:59 +0200774 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200775 self->cur = start;
776 self->next = next;
777 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200778 self->traverse = traverse;
779 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200781 return (PyObject *)(self);
782}
783
784 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200785IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200786{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200787 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200788 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200789 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790}
791
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200792 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200793IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200794{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200795 if (self->traverse != NULL)
796 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200797 else
798 return 0;
799}
800
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200801/* Mac OSX defines clear() somewhere. */
802#ifdef clear
803# undef clear
804#endif
805
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200806 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200807IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200808{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200809 if (self->clear != NULL)
810 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200811 else
812 return 0;
813}
814
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200815 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +0200816IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200817{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200818 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200819}
820
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200821 static PyObject *
822IterIter(PyObject *self)
823{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +0200824 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200825 return self;
826}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200827
Bram Moolenaardb913952012-06-29 12:54:53 +0200828typedef struct pylinkedlist_S {
829 struct pylinkedlist_S *pll_next;
830 struct pylinkedlist_S *pll_prev;
831 PyObject *pll_obj;
832} pylinkedlist_T;
833
834static pylinkedlist_T *lastdict = NULL;
835static pylinkedlist_T *lastlist = NULL;
836
837 static void
838pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
839{
840 if (ref->pll_prev == NULL)
841 {
842 if (ref->pll_next == NULL)
843 {
844 *last = NULL;
845 return;
846 }
847 }
848 else
849 ref->pll_prev->pll_next = ref->pll_next;
850
851 if (ref->pll_next == NULL)
852 *last = ref->pll_prev;
853 else
854 ref->pll_next->pll_prev = ref->pll_prev;
855}
856
857 static void
858pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
859{
860 if (*last == NULL)
861 ref->pll_prev = NULL;
862 else
863 {
864 (*last)->pll_next = ref;
865 ref->pll_prev = *last;
866 }
867 ref->pll_next = NULL;
868 ref->pll_obj = self;
869 *last = ref;
870}
871
872static PyTypeObject DictionaryType;
873
874typedef struct
875{
876 PyObject_HEAD
877 dict_T *dict;
878 pylinkedlist_T ref;
879} DictionaryObject;
880
Bram Moolenaara9922d62013-05-30 13:01:18 +0200881static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
882
883#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
884
Bram Moolenaardb913952012-06-29 12:54:53 +0200885 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +0200886DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +0200887{
888 DictionaryObject *self;
889
Bram Moolenaara9922d62013-05-30 13:01:18 +0200890 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200891 if (self == NULL)
892 return NULL;
893 self->dict = dict;
894 ++dict->dv_refcount;
895
896 pyll_add((PyObject *)(self), &self->ref, &lastdict);
897
898 return (PyObject *)(self);
899}
900
Bram Moolenaara9922d62013-05-30 13:01:18 +0200901 static dict_T *
902py_dict_alloc()
903{
904 dict_T *r;
905
906 if (!(r = dict_alloc()))
907 {
908 PyErr_NoMemory();
909 return NULL;
910 }
911 ++r->dv_refcount;
912
913 return r;
914}
915
916 static PyObject *
917DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
918{
919 DictionaryObject *self;
920 dict_T *dict;
921
922 if (!(dict = py_dict_alloc()))
923 return NULL;
924
925 self = (DictionaryObject *) DictionaryNew(subtype, dict);
926
927 --dict->dv_refcount;
928
929 if (kwargs || PyTuple_Size(args))
930 {
931 PyObject *tmp;
932 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
933 {
934 Py_DECREF(self);
935 return NULL;
936 }
937
938 Py_DECREF(tmp);
939 }
940
941 return (PyObject *)(self);
942}
943
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200944 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200945DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200946{
Bram Moolenaard6e39182013-05-21 18:30:34 +0200947 pyll_remove(&self->ref, &lastdict);
948 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200949
950 DESTRUCTOR_FINISH(self);
951}
952
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200953static char *DictionaryAttrs[] = {
954 "locked", "scope",
955 NULL
956};
957
958 static PyObject *
959DictionaryDir(PyObject *self)
960{
961 return ObjectDir(self, DictionaryAttrs);
962}
963
Bram Moolenaardb913952012-06-29 12:54:53 +0200964 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +0200965DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200966{
967 if (val == NULL)
968 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200969 PyErr_SetString(PyExc_AttributeError,
970 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200971 return -1;
972 }
973
974 if (strcmp(name, "locked") == 0)
975 {
Bram Moolenaard6e39182013-05-21 18:30:34 +0200976 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200977 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200978 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200979 return -1;
980 }
981 else
982 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200983 int istrue = PyObject_IsTrue(val);
984 if (istrue == -1)
985 return -1;
986 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +0200987 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200988 else
Bram Moolenaard6e39182013-05-21 18:30:34 +0200989 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200990 }
991 return 0;
992 }
993 else
994 {
Bram Moolenaara9922d62013-05-30 13:01:18 +0200995 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +0200996 return -1;
997 }
998}
999
1000 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001001DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001002{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001003 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001004}
1005
Bram Moolenaara9922d62013-05-30 13:01:18 +02001006#define DICT_FLAG_HAS_DEFAULT 0x01
1007#define DICT_FLAG_POP 0x02
1008#define DICT_FLAG_NONE_DEFAULT 0x04
1009#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1010#define DICT_FLAG_RETURN_PAIR 0x10
1011
Bram Moolenaardb913952012-06-29 12:54:53 +02001012 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001013_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001014{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001015 PyObject *keyObject;
1016 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1017 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001018 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001019 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001020 dict_T *dict = self->dict;
1021 hashitem_T *hi;
1022
Bram Moolenaardb913952012-06-29 12:54:53 +02001023 DICTKEY_DECL
1024
Bram Moolenaara9922d62013-05-30 13:01:18 +02001025 if (flags & DICT_FLAG_HAS_DEFAULT)
1026 {
1027 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1028 return NULL;
1029 }
1030 else
1031 keyObject = args;
1032
1033 if (flags & DICT_FLAG_RETURN_BOOL)
1034 defObject = Py_False;
1035
Bram Moolenaara03e6312013-05-29 22:49:26 +02001036 DICTKEY_GET(NULL, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001037
Bram Moolenaara9922d62013-05-30 13:01:18 +02001038 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001039
Bram Moolenaar696c2112012-09-21 13:43:14 +02001040 DICTKEY_UNREF
1041
Bram Moolenaara9922d62013-05-30 13:01:18 +02001042 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001043 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001044 if (defObject)
1045 {
1046 Py_INCREF(defObject);
1047 return defObject;
1048 }
1049 else
1050 {
1051 PyErr_SetObject(PyExc_KeyError, keyObject);
1052 return NULL;
1053 }
1054 }
1055 else if (flags & DICT_FLAG_RETURN_BOOL)
1056 {
1057 Py_INCREF(Py_True);
1058 return Py_True;
1059 }
1060
1061 di = dict_lookup(hi);
1062
1063 if (!(r = ConvertToPyObject(&di->di_tv)))
1064 return NULL;
1065
1066 if (flags & DICT_FLAG_POP)
1067 {
1068 if (dict->dv_lock)
1069 {
1070 PyErr_SetVim(_("dict is locked"));
1071 Py_DECREF(r);
1072 return NULL;
1073 }
1074
1075 hash_remove(&dict->dv_hashtab, hi);
1076 dictitem_free(di);
1077 }
1078
1079 if (flags & DICT_FLAG_RETURN_PAIR)
1080 {
1081 PyObject *tmp = r;
1082
1083 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, tmp)))
1084 {
1085 Py_DECREF(tmp);
1086 return NULL;
1087 }
1088 }
1089
1090 return r;
1091}
1092
1093 static PyObject *
1094DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1095{
1096 return _DictionaryItem(self, keyObject, 0);
1097}
1098
1099 static int
1100DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1101{
1102 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1103 int r;
1104
1105 r = (rObj == Py_True);
1106
1107 Py_DECREF(Py_True);
1108
1109 return r;
1110}
1111
1112typedef struct
1113{
1114 hashitem_T *ht_array;
1115 long_u ht_used;
1116 hashtab_T *ht;
1117 hashitem_T *hi;
1118 int todo;
1119} dictiterinfo_T;
1120
1121 static PyObject *
1122DictionaryIterNext(dictiterinfo_T **dii)
1123{
1124 PyObject *r;
1125
1126 if (!(*dii)->todo)
1127 return NULL;
1128
1129 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1130 (*dii)->ht->ht_used != (*dii)->ht_used)
1131 {
1132 PyErr_SetString(PyExc_RuntimeError,
1133 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001134 return NULL;
1135 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001136
Bram Moolenaara9922d62013-05-30 13:01:18 +02001137 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1138 ++((*dii)->hi);
1139
1140 --((*dii)->todo);
1141
1142 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1143 return NULL;
1144
1145 return r;
1146}
1147
1148 static PyObject *
1149DictionaryIter(DictionaryObject *self)
1150{
1151 dictiterinfo_T *dii;
1152 hashtab_T *ht;
1153
1154 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1155 {
1156 PyErr_NoMemory();
1157 return NULL;
1158 }
1159
1160 ht = &self->dict->dv_hashtab;
1161 dii->ht_array = ht->ht_array;
1162 dii->ht_used = ht->ht_used;
1163 dii->ht = ht;
1164 dii->hi = dii->ht_array;
1165 dii->todo = dii->ht_used;
1166
1167 return IterNew(dii,
1168 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1169 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001170}
1171
1172 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001173DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001174{
1175 char_u *key;
1176 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001177 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001178 dictitem_T *di;
1179 DICTKEY_DECL
1180
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001181 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001182 {
1183 PyErr_SetVim(_("dict is locked"));
1184 return -1;
1185 }
1186
Bram Moolenaara03e6312013-05-29 22:49:26 +02001187 DICTKEY_GET(-1, 0)
Bram Moolenaardb913952012-06-29 12:54:53 +02001188
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001189 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190
1191 if (valObject == NULL)
1192 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001193 hashitem_T *hi;
1194
Bram Moolenaardb913952012-06-29 12:54:53 +02001195 if (di == NULL)
1196 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001197 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001198 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001199 return -1;
1200 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001201 hi = hash_find(&dict->dv_hashtab, di->di_key);
1202 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001203 dictitem_free(di);
1204 return 0;
1205 }
1206
1207 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001208 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02001209
1210 if (di == NULL)
1211 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001212 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001213 {
1214 PyErr_NoMemory();
1215 return -1;
1216 }
1217 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001218 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001219
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001220 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001221 {
Bram Moolenaar696c2112012-09-21 13:43:14 +02001222 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +02001223 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001224 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001225 PyErr_SetVim(_("failed to add key to dictionary"));
1226 return -1;
1227 }
1228 }
1229 else
1230 clear_tv(&di->di_tv);
1231
1232 DICTKEY_UNREF
1233
1234 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001235 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 return 0;
1237}
1238
Bram Moolenaara9922d62013-05-30 13:01:18 +02001239typedef PyObject *(*hi_to_py)(hashitem_T *);
1240
Bram Moolenaardb913952012-06-29 12:54:53 +02001241 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001242DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001243{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001244 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001245 long_u todo = dict->dv_hashtab.ht_used;
1246 Py_ssize_t i = 0;
1247 PyObject *r;
1248 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001249 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001250
1251 r = PyList_New(todo);
1252 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1253 {
1254 if (!HASHITEM_EMPTY(hi))
1255 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001256 if (!(newObj = hiconvert(hi)))
1257 {
1258 Py_DECREF(r);
1259 return NULL;
1260 }
1261 if (PyList_SetItem(r, i, newObj))
1262 {
1263 Py_DECREF(r);
1264 Py_DECREF(newObj);
1265 return NULL;
1266 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001267 --todo;
1268 ++i;
1269 }
1270 }
1271 return r;
1272}
1273
Bram Moolenaara9922d62013-05-30 13:01:18 +02001274 static PyObject *
1275dict_key(hashitem_T *hi)
1276{
1277 return PyBytes_FromString((char *)(hi->hi_key));
1278}
1279
1280 static PyObject *
1281DictionaryListKeys(DictionaryObject *self)
1282{
1283 return DictionaryListObjects(self, dict_key);
1284}
1285
1286 static PyObject *
1287dict_val(hashitem_T *hi)
1288{
1289 dictitem_T *di;
1290
1291 di = dict_lookup(hi);
1292 return ConvertToPyObject(&di->di_tv);
1293}
1294
1295 static PyObject *
1296DictionaryListValues(DictionaryObject *self)
1297{
1298 return DictionaryListObjects(self, dict_val);
1299}
1300
1301 static PyObject *
1302dict_item(hashitem_T *hi)
1303{
1304 PyObject *keyObject;
1305 PyObject *valObject;
1306 PyObject *r;
1307
1308 if (!(keyObject = dict_key(hi)))
1309 return NULL;
1310
1311 if (!(valObject = dict_val(hi)))
1312 {
1313 Py_DECREF(keyObject);
1314 return NULL;
1315 }
1316
1317 r = Py_BuildValue("(OO)", keyObject, valObject);
1318
1319 Py_DECREF(keyObject);
1320 Py_DECREF(valObject);
1321
1322 return r;
1323}
1324
1325 static PyObject *
1326DictionaryListItems(DictionaryObject *self)
1327{
1328 return DictionaryListObjects(self, dict_item);
1329}
1330
1331 static PyObject *
1332DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1333{
1334 dict_T *dict = self->dict;
1335
1336 if (dict->dv_lock)
1337 {
1338 PyErr_SetVim(_("dict is locked"));
1339 return NULL;
1340 }
1341
1342 if (kwargs)
1343 {
1344 typval_T tv;
1345
1346 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1347 return NULL;
1348
1349 VimTryStart();
1350 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1351 clear_tv(&tv);
1352 if (VimTryEnd())
1353 return NULL;
1354 }
1355 else
1356 {
1357 PyObject *object;
1358
1359 if (!PyArg_Parse(args, "(O)", &object))
1360 return NULL;
1361
1362 if (PyObject_HasAttrString(object, "keys"))
1363 return DictionaryUpdate(self, NULL, object);
1364 else
1365 {
1366 PyObject *iterator;
1367 PyObject *item;
1368
1369 if (!(iterator = PyObject_GetIter(object)))
1370 return NULL;
1371
1372 while ((item = PyIter_Next(iterator)))
1373 {
1374 PyObject *fast;
1375 PyObject *keyObject;
1376 PyObject *valObject;
1377 PyObject *todecref;
1378 char_u *key;
1379 dictitem_T *di;
1380
1381 if (!(fast = PySequence_Fast(item, "")))
1382 {
1383 Py_DECREF(iterator);
1384 Py_DECREF(item);
1385 return NULL;
1386 }
1387
1388 Py_DECREF(item);
1389
1390 if (PySequence_Fast_GET_SIZE(fast) != 2)
1391 {
1392 Py_DECREF(iterator);
1393 Py_DECREF(fast);
1394 PyErr_SetString(PyExc_ValueError,
1395 _("expected sequence element of size 2"));
1396 return NULL;
1397 }
1398
1399 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1400
1401 if (!(key = StringToChars(keyObject, &todecref)))
1402 {
1403 Py_DECREF(iterator);
1404 Py_DECREF(fast);
1405 return NULL;
1406 }
1407
1408 di = dictitem_alloc(key);
1409
1410 Py_XDECREF(todecref);
1411
1412 if (di == NULL)
1413 {
1414 Py_DECREF(fast);
1415 Py_DECREF(iterator);
1416 PyErr_NoMemory();
1417 return NULL;
1418 }
1419 di->di_tv.v_lock = 0;
1420 di->di_tv.v_type = VAR_UNKNOWN;
1421
1422 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1423
1424 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1425 {
1426 Py_DECREF(iterator);
1427 Py_DECREF(fast);
1428 dictitem_free(di);
1429 return NULL;
1430 }
1431
1432 Py_DECREF(fast);
1433
1434 if (dict_add(dict, di) == FAIL)
1435 {
1436 Py_DECREF(iterator);
1437 dictitem_free(di);
1438 PyErr_SetVim(_("failed to add key to dictionary"));
1439 return NULL;
1440 }
1441 }
1442
1443 Py_DECREF(iterator);
1444
1445 /* Iterator may have finished due to an exception */
1446 if (PyErr_Occurred())
1447 return NULL;
1448 }
1449 }
1450 Py_INCREF(Py_None);
1451 return Py_None;
1452}
1453
1454 static PyObject *
1455DictionaryGet(DictionaryObject *self, PyObject *args)
1456{
1457 return _DictionaryItem(self, args,
1458 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1459}
1460
1461 static PyObject *
1462DictionaryPop(DictionaryObject *self, PyObject *args)
1463{
1464 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1465}
1466
1467 static PyObject *
1468DictionaryPopItem(DictionaryObject *self, PyObject *args)
1469{
1470 PyObject *keyObject;
1471
1472 if (!PyArg_ParseTuple(args, "O", &keyObject))
1473 return NULL;
1474
1475 return _DictionaryItem(self, keyObject,
1476 DICT_FLAG_POP|DICT_FLAG_RETURN_PAIR);
1477}
1478
1479 static PyObject *
1480DictionaryHasKey(DictionaryObject *self, PyObject *args)
1481{
1482 PyObject *keyObject;
1483
1484 if (!PyArg_ParseTuple(args, "O", &keyObject))
1485 return NULL;
1486
1487 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1488}
1489
1490static PySequenceMethods DictionaryAsSeq = {
1491 0, /* sq_length */
1492 0, /* sq_concat */
1493 0, /* sq_repeat */
1494 0, /* sq_item */
1495 0, /* sq_slice */
1496 0, /* sq_ass_item */
1497 0, /* sq_ass_slice */
1498 (objobjproc) DictionaryContains, /* sq_contains */
1499 0, /* sq_inplace_concat */
1500 0, /* sq_inplace_repeat */
1501};
1502
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001503static PyMappingMethods DictionaryAsMapping = {
1504 (lenfunc) DictionaryLength,
1505 (binaryfunc) DictionaryItem,
1506 (objobjargproc) DictionaryAssItem,
1507};
1508
Bram Moolenaardb913952012-06-29 12:54:53 +02001509static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001510 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001511 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1512 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1513 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1514 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1515 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
1516 {"popitem", (PyCFunction)DictionaryPopItem, METH_VARARGS, ""},
1517 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001518 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1519 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001520};
1521
1522static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001523static PySequenceMethods ListAsSeq;
1524static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001525
1526typedef struct
1527{
1528 PyObject_HEAD
1529 list_T *list;
1530 pylinkedlist_T ref;
1531} ListObject;
1532
1533 static PyObject *
1534ListNew(list_T *list)
1535{
1536 ListObject *self;
1537
1538 self = PyObject_NEW(ListObject, &ListType);
1539 if (self == NULL)
1540 return NULL;
1541 self->list = list;
1542 ++list->lv_refcount;
1543
1544 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1545
1546 return (PyObject *)(self);
1547}
1548
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001549 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001550ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001551{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001552 pyll_remove(&self->ref, &lastlist);
1553 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001554
1555 DESTRUCTOR_FINISH(self);
1556}
1557
Bram Moolenaardb913952012-06-29 12:54:53 +02001558 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001559list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001560{
1561 Py_ssize_t i;
1562 Py_ssize_t lsize = PySequence_Size(obj);
1563 PyObject *litem;
1564 listitem_T *li;
1565
1566 for(i=0; i<lsize; i++)
1567 {
1568 li = listitem_alloc();
1569 if (li == NULL)
1570 {
1571 PyErr_NoMemory();
1572 return -1;
1573 }
1574 li->li_tv.v_lock = 0;
1575
1576 litem = PySequence_GetItem(obj, i);
1577 if (litem == NULL)
1578 return -1;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001579 if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02001580 return -1;
1581
1582 list_append(l, li);
1583 }
1584 return 0;
1585}
1586
Bram Moolenaardb913952012-06-29 12:54:53 +02001587 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001588ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001589{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001591}
1592
1593 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001594ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001595{
1596 listitem_T *li;
1597
Bram Moolenaard6e39182013-05-21 18:30:34 +02001598 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001599 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001600 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001601 return NULL;
1602 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001603 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001604 if (li == NULL)
1605 {
1606 PyErr_SetVim(_("internal error: failed to get vim list item"));
1607 return NULL;
1608 }
1609 return ConvertToPyObject(&li->li_tv);
1610}
1611
1612#define PROC_RANGE \
1613 if (last < 0) {\
1614 if (last < -size) \
1615 last = 0; \
1616 else \
1617 last += size; \
1618 } \
1619 if (first < 0) \
1620 first = 0; \
1621 if (first > size) \
1622 first = size; \
1623 if (last > size) \
1624 last = size;
1625
1626 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001627ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001628{
1629 PyInt i;
1630 PyInt size = ListLength(self);
1631 PyInt n;
1632 PyObject *list;
1633 int reversed = 0;
1634
1635 PROC_RANGE
1636 if (first >= last)
1637 first = last;
1638
1639 n = last-first;
1640 list = PyList_New(n);
1641 if (list == NULL)
1642 return NULL;
1643
1644 for (i = 0; i < n; ++i)
1645 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001646 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001647 if (item == NULL)
1648 {
1649 Py_DECREF(list);
1650 return NULL;
1651 }
1652
1653 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1654 {
1655 Py_DECREF(item);
1656 Py_DECREF(list);
1657 return NULL;
1658 }
1659 }
1660
1661 return list;
1662}
1663
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001664typedef struct
1665{
1666 listwatch_T lw;
1667 list_T *list;
1668} listiterinfo_T;
1669
1670 static void
1671ListIterDestruct(listiterinfo_T *lii)
1672{
1673 list_rem_watch(lii->list, &lii->lw);
1674 PyMem_Free(lii);
1675}
1676
1677 static PyObject *
1678ListIterNext(listiterinfo_T **lii)
1679{
1680 PyObject *r;
1681
1682 if (!((*lii)->lw.lw_item))
1683 return NULL;
1684
1685 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1686 return NULL;
1687
1688 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1689
1690 return r;
1691}
1692
1693 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001694ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001695{
1696 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001697 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001698
1699 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1700 {
1701 PyErr_NoMemory();
1702 return NULL;
1703 }
1704
1705 list_add_watch(l, &lii->lw);
1706 lii->lw.lw_item = l->lv_first;
1707 lii->list = l;
1708
1709 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001710 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1711 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001712}
1713
Bram Moolenaardb913952012-06-29 12:54:53 +02001714 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001715ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001716{
1717 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001718 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001719 listitem_T *li;
1720 Py_ssize_t length = ListLength(self);
1721
1722 if (l->lv_lock)
1723 {
1724 PyErr_SetVim(_("list is locked"));
1725 return -1;
1726 }
1727 if (index>length || (index==length && obj==NULL))
1728 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001729 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001730 return -1;
1731 }
1732
1733 if (obj == NULL)
1734 {
1735 li = list_find(l, (long) index);
1736 list_remove(l, li, li);
1737 clear_tv(&li->li_tv);
1738 vim_free(li);
1739 return 0;
1740 }
1741
1742 if (ConvertFromPyObject(obj, &tv) == -1)
1743 return -1;
1744
1745 if (index == length)
1746 {
1747 if (list_append_tv(l, &tv) == FAIL)
1748 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001749 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 PyErr_SetVim(_("Failed to add item to list"));
1751 return -1;
1752 }
1753 }
1754 else
1755 {
1756 li = list_find(l, (long) index);
1757 clear_tv(&li->li_tv);
1758 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001759 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 }
1761 return 0;
1762}
1763
1764 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001765ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001766{
1767 PyInt size = ListLength(self);
1768 Py_ssize_t i;
1769 Py_ssize_t lsize;
1770 PyObject *litem;
1771 listitem_T *li;
1772 listitem_T *next;
1773 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001774 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001775
1776 if (l->lv_lock)
1777 {
1778 PyErr_SetVim(_("list is locked"));
1779 return -1;
1780 }
1781
1782 PROC_RANGE
1783
1784 if (first == size)
1785 li = NULL;
1786 else
1787 {
1788 li = list_find(l, (long) first);
1789 if (li == NULL)
1790 {
1791 PyErr_SetVim(_("internal error: no vim list item"));
1792 return -1;
1793 }
1794 if (last > first)
1795 {
1796 i = last - first;
1797 while (i-- && li != NULL)
1798 {
1799 next = li->li_next;
1800 listitem_remove(l, li);
1801 li = next;
1802 }
1803 }
1804 }
1805
1806 if (obj == NULL)
1807 return 0;
1808
1809 if (!PyList_Check(obj))
1810 {
1811 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1812 return -1;
1813 }
1814
1815 lsize = PyList_Size(obj);
1816
1817 for(i=0; i<lsize; i++)
1818 {
1819 litem = PyList_GetItem(obj, i);
1820 if (litem == NULL)
1821 return -1;
1822 if (ConvertFromPyObject(litem, &v) == -1)
1823 return -1;
1824 if (list_insert_tv(l, &v, li) == FAIL)
1825 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001826 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001827 PyErr_SetVim(_("internal error: failed to add item to list"));
1828 return -1;
1829 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001830 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 }
1832 return 0;
1833}
1834
1835 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001836ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02001837{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001838 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02001839 PyObject *lookup_dict;
1840
1841 if (l->lv_lock)
1842 {
1843 PyErr_SetVim(_("list is locked"));
1844 return NULL;
1845 }
1846
1847 if (!PySequence_Check(obj))
1848 {
1849 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1850 return NULL;
1851 }
1852
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001853 if (!(lookup_dict = PyDict_New()))
1854 return NULL;
1855
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 if (list_py_concat(l, obj, lookup_dict) == -1)
1857 {
1858 Py_DECREF(lookup_dict);
1859 return NULL;
1860 }
1861 Py_DECREF(lookup_dict);
1862
1863 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001864 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001865}
1866
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001867static char *ListAttrs[] = {
1868 "locked",
1869 NULL
1870};
1871
1872 static PyObject *
1873ListDir(PyObject *self)
1874{
1875 return ObjectDir(self, ListAttrs);
1876}
1877
Bram Moolenaar66b79852012-09-21 14:00:35 +02001878 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001879ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001880{
1881 if (val == NULL)
1882 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001883 PyErr_SetString(PyExc_AttributeError,
1884 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001885 return -1;
1886 }
1887
1888 if (strcmp(name, "locked") == 0)
1889 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001890 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001891 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001892 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001893 return -1;
1894 }
1895 else
1896 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001897 int istrue = PyObject_IsTrue(val);
1898 if (istrue == -1)
1899 return -1;
1900 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001901 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001902 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001903 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001904 }
1905 return 0;
1906 }
1907 else
1908 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001909 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001910 return -1;
1911 }
1912}
1913
Bram Moolenaardb913952012-06-29 12:54:53 +02001914static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001915 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1916 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
1917 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001918};
1919
1920typedef struct
1921{
1922 PyObject_HEAD
1923 char_u *name;
1924} FunctionObject;
1925
1926static PyTypeObject FunctionType;
1927
1928 static PyObject *
1929FunctionNew(char_u *name)
1930{
1931 FunctionObject *self;
1932
1933 self = PyObject_NEW(FunctionObject, &FunctionType);
1934 if (self == NULL)
1935 return NULL;
1936 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1937 if (self->name == NULL)
1938 {
1939 PyErr_NoMemory();
1940 return NULL;
1941 }
1942 STRCPY(self->name, name);
1943 func_ref(name);
1944 return (PyObject *)(self);
1945}
1946
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001947 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001948FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001949{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001950 func_unref(self->name);
1951 PyMem_Free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001952
1953 DESTRUCTOR_FINISH(self);
1954}
1955
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001956static char *FunctionAttrs[] = {
1957 "softspace",
1958 NULL
1959};
1960
1961 static PyObject *
1962FunctionDir(PyObject *self)
1963{
1964 return ObjectDir(self, FunctionAttrs);
1965}
1966
Bram Moolenaardb913952012-06-29 12:54:53 +02001967 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001968FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02001969{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001970 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02001971 typval_T args;
1972 typval_T selfdicttv;
1973 typval_T rettv;
1974 dict_T *selfdict = NULL;
1975 PyObject *selfdictObject;
1976 PyObject *result;
1977 int error;
1978
1979 if (ConvertFromPyObject(argsObject, &args) == -1)
1980 return NULL;
1981
1982 if (kwargs != NULL)
1983 {
1984 selfdictObject = PyDict_GetItemString(kwargs, "self");
1985 if (selfdictObject != NULL)
1986 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001987 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001988 {
1989 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02001990 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001991 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001992 selfdict = selfdicttv.vval.v_dict;
1993 }
1994 }
1995
Bram Moolenaar71700b82013-05-15 17:49:05 +02001996 Py_BEGIN_ALLOW_THREADS
1997 Python_Lock_Vim();
1998
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001999 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002000 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002001
2002 Python_Release_Vim();
2003 Py_END_ALLOW_THREADS
2004
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002005 if (VimTryEnd())
2006 result = NULL;
2007 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002008 {
2009 result = NULL;
2010 PyErr_SetVim(_("failed to run function"));
2011 }
2012 else
2013 result = ConvertToPyObject(&rettv);
2014
Bram Moolenaardb913952012-06-29 12:54:53 +02002015 clear_tv(&args);
2016 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002017 if (selfdict != NULL)
2018 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002019
2020 return result;
2021}
2022
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002023 static PyObject *
2024FunctionRepr(FunctionObject *self)
2025{
2026 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2027}
2028
Bram Moolenaardb913952012-06-29 12:54:53 +02002029static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002030 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
2031 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2032 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002033};
2034
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002035/*
2036 * Options object
2037 */
2038
2039static PyTypeObject OptionsType;
2040
2041typedef int (*checkfun)(void *);
2042
2043typedef struct
2044{
2045 PyObject_HEAD
2046 int opt_type;
2047 void *from;
2048 checkfun Check;
2049 PyObject *fromObj;
2050} OptionsObject;
2051
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002052 static int
2053dummy_check(void *arg UNUSED)
2054{
2055 return 0;
2056}
2057
2058 static PyObject *
2059OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2060{
2061 OptionsObject *self;
2062
Bram Moolenaar774267b2013-05-21 20:51:59 +02002063 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002064 if (self == NULL)
2065 return NULL;
2066
2067 self->opt_type = opt_type;
2068 self->from = from;
2069 self->Check = Check;
2070 self->fromObj = fromObj;
2071 if (fromObj)
2072 Py_INCREF(fromObj);
2073
2074 return (PyObject *)(self);
2075}
2076
2077 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002078OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002079{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002080 PyObject_GC_UnTrack((void *)(self));
2081 Py_XDECREF(self->fromObj);
2082 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002083}
2084
2085 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002086OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002088 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002089 return 0;
2090}
2091
2092 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002093OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002094{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002095 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002096 return 0;
2097}
2098
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002099 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002100OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002101{
2102 char_u *key;
2103 int flags;
2104 long numval;
2105 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002106 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002107
Bram Moolenaard6e39182013-05-21 18:30:34 +02002108 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002109 return NULL;
2110
Bram Moolenaara03e6312013-05-29 22:49:26 +02002111 DICTKEY_GET(NULL, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002112
2113 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002114 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002115
2116 DICTKEY_UNREF
2117
2118 if (flags == 0)
2119 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002120 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002121 return NULL;
2122 }
2123
2124 if (flags & SOPT_UNSET)
2125 {
2126 Py_INCREF(Py_None);
2127 return Py_None;
2128 }
2129 else if (flags & SOPT_BOOL)
2130 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002131 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002132 r = numval ? Py_True : Py_False;
2133 Py_INCREF(r);
2134 return r;
2135 }
2136 else if (flags & SOPT_NUM)
2137 return PyInt_FromLong(numval);
2138 else if (flags & SOPT_STRING)
2139 {
2140 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002141 {
2142 PyObject *r = PyBytes_FromString((char *) stringval);
2143 vim_free(stringval);
2144 return r;
2145 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002146 else
2147 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002148 PyErr_SetString(PyExc_RuntimeError,
2149 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002150 return NULL;
2151 }
2152 }
2153 else
2154 {
2155 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2156 return NULL;
2157 }
2158}
2159
2160 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002161set_option_value_err(key, numval, stringval, opt_flags)
2162 char_u *key;
2163 int numval;
2164 char_u *stringval;
2165 int opt_flags;
2166{
2167 char_u *errmsg;
2168
2169 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2170 {
2171 if (VimTryEnd())
2172 return FAIL;
2173 PyErr_SetVim((char *)errmsg);
2174 return FAIL;
2175 }
2176 return OK;
2177}
2178
2179 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002180set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2181 char_u *key;
2182 int numval;
2183 char_u *stringval;
2184 int opt_flags;
2185 int opt_type;
2186 void *from;
2187{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002188 win_T *save_curwin = NULL;
2189 tabpage_T *save_curtab = NULL;
2190 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002191 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002192
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002193 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002194 switch (opt_type)
2195 {
2196 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002197 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2198 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002199 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002200 if (VimTryEnd())
2201 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002202 PyErr_SetVim("Problem while switching windows.");
2203 return -1;
2204 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002205 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002206 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002207 if (r == FAIL)
2208 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002209 break;
2210 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002211 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002212 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002213 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002214 if (r == FAIL)
2215 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002216 break;
2217 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002218 r = set_option_value_err(key, numval, stringval, opt_flags);
2219 if (r == FAIL)
2220 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002221 break;
2222 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002223 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002224}
2225
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002226 static void *
2227py_memsave(void *p, size_t len)
2228{
2229 void *r;
2230 if (!(r = PyMem_Malloc(len)))
2231 return NULL;
2232 mch_memmove(r, p, len);
2233 return r;
2234}
2235
2236#define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
2237
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002238 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002239OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002240{
2241 char_u *key;
2242 int flags;
2243 int opt_flags;
2244 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02002245 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002246
Bram Moolenaard6e39182013-05-21 18:30:34 +02002247 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002248 return -1;
2249
Bram Moolenaara03e6312013-05-29 22:49:26 +02002250 DICTKEY_GET(-1, 0)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002251
2252 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002253 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002254
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002255 if (flags == 0)
2256 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002257 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002258 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002259 return -1;
2260 }
2261
2262 if (valObject == NULL)
2263 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002265 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002266 PyErr_SetString(PyExc_ValueError,
2267 _("unable to unset global option"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002268 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002269 return -1;
2270 }
2271 else if (!(flags & SOPT_GLOBAL))
2272 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002273 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2274 "without global value"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002275 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002276 return -1;
2277 }
2278 else
2279 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002280 unset_global_local_option(key, self->from);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002281 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002282 return 0;
2283 }
2284 }
2285
Bram Moolenaard6e39182013-05-21 18:30:34 +02002286 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002287
2288 if (flags & SOPT_BOOL)
2289 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002290 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002291
Bram Moolenaarb983f752013-05-15 16:11:50 +02002292 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002293 r = -1;
2294 else
2295 r = set_option_value_for(key, istrue, NULL,
2296 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002297 }
2298 else if (flags & SOPT_NUM)
2299 {
2300 int val;
2301
2302#if PY_MAJOR_VERSION < 3
2303 if (PyInt_Check(valObject))
2304 val = PyInt_AsLong(valObject);
2305 else
2306#endif
2307 if (PyLong_Check(valObject))
2308 val = PyLong_AsLong(valObject);
2309 else
2310 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002311 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002312 DICTKEY_UNREF
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002313 return -1;
2314 }
2315
2316 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002317 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002318 }
2319 else
2320 {
2321 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002322 PyObject *todecref;
2323
2324 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002325 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002326 r = set_option_value_for(key, 0, val, opt_flags,
2327 self->opt_type, self->from);
2328 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002329 }
2330 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002331 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002332 }
2333
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002334 DICTKEY_UNREF
2335
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002336 return r;
2337}
2338
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002339static PyMappingMethods OptionsAsMapping = {
2340 (lenfunc) NULL,
2341 (binaryfunc) OptionsItem,
2342 (objobjargproc) OptionsAssItem,
2343};
2344
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002345/* Tabpage object
2346 */
2347
2348typedef struct
2349{
2350 PyObject_HEAD
2351 tabpage_T *tab;
2352} TabPageObject;
2353
2354static PyObject *WinListNew(TabPageObject *tabObject);
2355
2356static PyTypeObject TabPageType;
2357
2358 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002359CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002361 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002362 {
2363 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2364 return -1;
2365 }
2366
2367 return 0;
2368}
2369
2370 static PyObject *
2371TabPageNew(tabpage_T *tab)
2372{
2373 TabPageObject *self;
2374
2375 if (TAB_PYTHON_REF(tab))
2376 {
2377 self = TAB_PYTHON_REF(tab);
2378 Py_INCREF(self);
2379 }
2380 else
2381 {
2382 self = PyObject_NEW(TabPageObject, &TabPageType);
2383 if (self == NULL)
2384 return NULL;
2385 self->tab = tab;
2386 TAB_PYTHON_REF(tab) = self;
2387 }
2388
2389 return (PyObject *)(self);
2390}
2391
2392 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002393TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002394{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002395 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2396 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002397
2398 DESTRUCTOR_FINISH(self);
2399}
2400
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002401static char *TabPageAttrs[] = {
2402 "windows", "number", "vars", "window", "valid",
2403 NULL
2404};
2405
2406 static PyObject *
2407TabPageDir(PyObject *self)
2408{
2409 return ObjectDir(self, TabPageAttrs);
2410}
2411
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002412 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002413TabPageAttrValid(TabPageObject *self, char *name)
2414{
2415 PyObject *r;
2416
2417 if (strcmp(name, "valid") != 0)
2418 return NULL;
2419
2420 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2421 Py_INCREF(r);
2422 return r;
2423}
2424
2425 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002426TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002427{
2428 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002429 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002430 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002432 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002433 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002434 else if (strcmp(name, "window") == 0)
2435 {
2436 /* For current tab window.c does not bother to set or update tp_curwin
2437 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002438 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002439 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002440 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002441 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002442 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002443 else if (strcmp(name, "__members__") == 0)
2444 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002445 return NULL;
2446}
2447
2448 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002449TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002450{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002451 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002452 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002453 else
2454 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002455 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002456
2457 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002458 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2459 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002460 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002461 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002462 }
2463}
2464
2465static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002466 /* name, function, calling, documentation */
2467 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2468 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002469};
2470
2471/*
2472 * Window list object
2473 */
2474
2475static PyTypeObject TabListType;
2476static PySequenceMethods TabListAsSeq;
2477
2478typedef struct
2479{
2480 PyObject_HEAD
2481} TabListObject;
2482
2483 static PyInt
2484TabListLength(PyObject *self UNUSED)
2485{
2486 tabpage_T *tp = first_tabpage;
2487 PyInt n = 0;
2488
2489 while (tp != NULL)
2490 {
2491 ++n;
2492 tp = tp->tp_next;
2493 }
2494
2495 return n;
2496}
2497
2498 static PyObject *
2499TabListItem(PyObject *self UNUSED, PyInt n)
2500{
2501 tabpage_T *tp;
2502
2503 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2504 if (n == 0)
2505 return TabPageNew(tp);
2506
2507 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2508 return NULL;
2509}
2510
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002511/* Window object
2512 */
2513
2514typedef struct
2515{
2516 PyObject_HEAD
2517 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002518 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002519} WindowObject;
2520
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002521static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002522
2523 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002524CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002525{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002526 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002527 {
2528 PyErr_SetVim(_("attempt to refer to deleted window"));
2529 return -1;
2530 }
2531
2532 return 0;
2533}
2534
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002535 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002536WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002537{
2538 /* We need to handle deletion of windows underneath us.
2539 * If we add a "w_python*_ref" field to the win_T structure,
2540 * then we can get at it in win_free() in vim. We then
2541 * need to create only ONE Python object per window - if
2542 * we try to create a second, just INCREF the existing one
2543 * and return it. The (single) Python object referring to
2544 * the window is stored in "w_python*_ref".
2545 * On a win_free() we set the Python object's win_T* field
2546 * to an invalid value. We trap all uses of a window
2547 * object, and reject them if the win_T* field is invalid.
2548 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002549 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002550 * w_python_ref and w_python3_ref fields respectively.
2551 */
2552
2553 WindowObject *self;
2554
2555 if (WIN_PYTHON_REF(win))
2556 {
2557 self = WIN_PYTHON_REF(win);
2558 Py_INCREF(self);
2559 }
2560 else
2561 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002562 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002563 if (self == NULL)
2564 return NULL;
2565 self->win = win;
2566 WIN_PYTHON_REF(win) = self;
2567 }
2568
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002569 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2570
Bram Moolenaar971db462013-05-12 18:44:48 +02002571 return (PyObject *)(self);
2572}
2573
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002574 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002575WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002576{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002577 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002578 if (self->win && self->win != INVALID_WINDOW_VALUE)
2579 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002580 Py_XDECREF(((PyObject *)(self->tabObject)));
2581 PyObject_GC_Del((void *)(self));
2582}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002583
Bram Moolenaar774267b2013-05-21 20:51:59 +02002584 static int
2585WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2586{
2587 Py_VISIT(((PyObject *)(self->tabObject)));
2588 return 0;
2589}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002590
Bram Moolenaar774267b2013-05-21 20:51:59 +02002591 static int
2592WindowClear(WindowObject *self)
2593{
2594 Py_CLEAR(self->tabObject);
2595 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002596}
2597
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002598 static win_T *
2599get_firstwin(TabPageObject *tabObject)
2600{
2601 if (tabObject)
2602 {
2603 if (CheckTabPage(tabObject))
2604 return NULL;
2605 /* For current tab window.c does not bother to set or update tp_firstwin
2606 */
2607 else if (tabObject->tab == curtab)
2608 return firstwin;
2609 else
2610 return tabObject->tab->tp_firstwin;
2611 }
2612 else
2613 return firstwin;
2614}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002615static char *WindowAttrs[] = {
2616 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2617 "tabpage", "valid",
2618 NULL
2619};
2620
2621 static PyObject *
2622WindowDir(PyObject *self)
2623{
2624 return ObjectDir(self, WindowAttrs);
2625}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002626
Bram Moolenaar971db462013-05-12 18:44:48 +02002627 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002628WindowAttrValid(WindowObject *self, char *name)
2629{
2630 PyObject *r;
2631
2632 if (strcmp(name, "valid") != 0)
2633 return NULL;
2634
2635 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2636 Py_INCREF(r);
2637 return r;
2638}
2639
2640 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002641WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002642{
2643 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002644 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002645 else if (strcmp(name, "cursor") == 0)
2646 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002647 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002648
2649 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2650 }
2651 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002652 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002653#ifdef FEAT_WINDOWS
2654 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002655 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002656#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002657#ifdef FEAT_VERTSPLIT
2658 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002659 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002660 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002661 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002662#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002663 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002664 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002665 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002666 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2667 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02002668 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002669 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002670 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002671 return NULL;
2672 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002673 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002674 }
2675 else if (strcmp(name, "tabpage") == 0)
2676 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002677 Py_INCREF(self->tabObject);
2678 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002679 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002680 else if (strcmp(name, "__members__") == 0)
2681 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002682 else
2683 return NULL;
2684}
2685
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002686 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002687WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002688{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002689 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002690 return -1;
2691
2692 if (strcmp(name, "buffer") == 0)
2693 {
2694 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
2695 return -1;
2696 }
2697 else if (strcmp(name, "cursor") == 0)
2698 {
2699 long lnum;
2700 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002701
2702 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
2703 return -1;
2704
Bram Moolenaard6e39182013-05-21 18:30:34 +02002705 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002706 {
2707 PyErr_SetVim(_("cursor position outside buffer"));
2708 return -1;
2709 }
2710
2711 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002712 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002713 return -1;
2714
Bram Moolenaard6e39182013-05-21 18:30:34 +02002715 self->win->w_cursor.lnum = lnum;
2716 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002717#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02002718 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002719#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002720 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002721 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002722
Bram Moolenaar03a807a2011-07-07 15:08:58 +02002723 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002724 return 0;
2725 }
2726 else if (strcmp(name, "height") == 0)
2727 {
2728 int height;
2729 win_T *savewin;
2730
2731 if (!PyArg_Parse(val, "i", &height))
2732 return -1;
2733
2734#ifdef FEAT_GUI
2735 need_mouse_correct = TRUE;
2736#endif
2737 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002739
2740 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002741 win_setheight(height);
2742 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002743 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002744 return -1;
2745
2746 return 0;
2747 }
2748#ifdef FEAT_VERTSPLIT
2749 else if (strcmp(name, "width") == 0)
2750 {
2751 int width;
2752 win_T *savewin;
2753
2754 if (!PyArg_Parse(val, "i", &width))
2755 return -1;
2756
2757#ifdef FEAT_GUI
2758 need_mouse_correct = TRUE;
2759#endif
2760 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002761 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002762
2763 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002764 win_setwidth(width);
2765 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002766 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002767 return -1;
2768
2769 return 0;
2770 }
2771#endif
2772 else
2773 {
2774 PyErr_SetString(PyExc_AttributeError, name);
2775 return -1;
2776 }
2777}
2778
2779 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002781{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002782 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002783 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002784 else
2785 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002786 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002787
Bram Moolenaar6d216452013-05-12 19:00:41 +02002788 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002789 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002790 (self));
2791 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002792 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002793 }
2794}
2795
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002796static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002797 /* name, function, calling, documentation */
2798 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
2799 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002800};
2801
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002802/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002803 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002804 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002805
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002806static PyTypeObject WinListType;
2807static PySequenceMethods WinListAsSeq;
2808
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002809typedef struct
2810{
2811 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002812 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002813} WinListObject;
2814
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002815 static PyObject *
2816WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002817{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002818 WinListObject *self;
2819
2820 self = PyObject_NEW(WinListObject, &WinListType);
2821 self->tabObject = tabObject;
2822 Py_INCREF(tabObject);
2823
2824 return (PyObject *)(self);
2825}
2826
2827 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002828WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002829{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002830 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002831
2832 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02002833 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002834 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02002835 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002836
2837 DESTRUCTOR_FINISH(self);
2838}
2839
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002840 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002841WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002842{
2843 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002844 PyInt n = 0;
2845
Bram Moolenaard6e39182013-05-21 18:30:34 +02002846 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002847 return -1;
2848
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002849 while (w != NULL)
2850 {
2851 ++n;
2852 w = W_NEXT(w);
2853 }
2854
2855 return n;
2856}
2857
2858 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002859WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002860{
2861 win_T *w;
2862
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002864 return NULL;
2865
2866 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002867 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002868 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002869
2870 PyErr_SetString(PyExc_IndexError, _("no such window"));
2871 return NULL;
2872}
2873
2874/* Convert a Python string into a Vim line.
2875 *
2876 * The result is in allocated memory. All internal nulls are replaced by
2877 * newline characters. It is an error for the string to contain newline
2878 * characters.
2879 *
2880 * On errors, the Python exception data is set, and NULL is returned.
2881 */
2882 static char *
2883StringToLine(PyObject *obj)
2884{
2885 const char *str;
2886 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002887 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002888 PyInt len;
2889 PyInt i;
2890 char *p;
2891
2892 if (obj == NULL || !PyString_Check(obj))
2893 {
2894 PyErr_BadArgument();
2895 return NULL;
2896 }
2897
Bram Moolenaar19e60942011-06-19 00:27:51 +02002898 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2899 str = PyString_AsString(bytes);
2900 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002901
2902 /*
2903 * Error checking: String must not contain newlines, as we
2904 * are replacing a single line, and we must replace it with
2905 * a single line.
2906 * A trailing newline is removed, so that append(f.readlines()) works.
2907 */
2908 p = memchr(str, '\n', len);
2909 if (p != NULL)
2910 {
2911 if (p == str + len - 1)
2912 --len;
2913 else
2914 {
2915 PyErr_SetVim(_("string cannot contain newlines"));
2916 return NULL;
2917 }
2918 }
2919
2920 /* Create a copy of the string, with internal nulls replaced by
2921 * newline characters, as is the vim convention.
2922 */
2923 save = (char *)alloc((unsigned)(len+1));
2924 if (save == NULL)
2925 {
2926 PyErr_NoMemory();
2927 return NULL;
2928 }
2929
2930 for (i = 0; i < len; ++i)
2931 {
2932 if (str[i] == '\0')
2933 save[i] = '\n';
2934 else
2935 save[i] = str[i];
2936 }
2937
2938 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002939 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002940
2941 return save;
2942}
2943
2944/* Get a line from the specified buffer. The line number is
2945 * in Vim format (1-based). The line is returned as a Python
2946 * string object.
2947 */
2948 static PyObject *
2949GetBufferLine(buf_T *buf, PyInt n)
2950{
2951 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2952}
2953
2954
2955/* Get a list of lines from the specified buffer. The line numbers
2956 * are in Vim format (1-based). The range is from lo up to, but not
2957 * including, hi. The list is returned as a Python list of string objects.
2958 */
2959 static PyObject *
2960GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2961{
2962 PyInt i;
2963 PyInt n = hi - lo;
2964 PyObject *list = PyList_New(n);
2965
2966 if (list == NULL)
2967 return NULL;
2968
2969 for (i = 0; i < n; ++i)
2970 {
2971 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2972
2973 /* Error check - was the Python string creation OK? */
2974 if (str == NULL)
2975 {
2976 Py_DECREF(list);
2977 return NULL;
2978 }
2979
2980 /* Set the list item */
2981 if (PyList_SetItem(list, i, str))
2982 {
2983 Py_DECREF(str);
2984 Py_DECREF(list);
2985 return NULL;
2986 }
2987 }
2988
2989 /* The ownership of the Python list is passed to the caller (ie,
2990 * the caller should Py_DECREF() the object when it is finished
2991 * with it).
2992 */
2993
2994 return list;
2995}
2996
2997/*
2998 * Check if deleting lines made the cursor position invalid.
2999 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3000 * deleted).
3001 */
3002 static void
3003py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3004{
3005 if (curwin->w_cursor.lnum >= lo)
3006 {
3007 /* Adjust the cursor position if it's in/after the changed
3008 * lines. */
3009 if (curwin->w_cursor.lnum >= hi)
3010 {
3011 curwin->w_cursor.lnum += extra;
3012 check_cursor_col();
3013 }
3014 else if (extra < 0)
3015 {
3016 curwin->w_cursor.lnum = lo;
3017 check_cursor();
3018 }
3019 else
3020 check_cursor_col();
3021 changed_cline_bef_curs();
3022 }
3023 invalidate_botline();
3024}
3025
Bram Moolenaar19e60942011-06-19 00:27:51 +02003026/*
3027 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003028 * in Vim format (1-based). The replacement line is given as
3029 * a Python string object. The object is checked for validity
3030 * and correct format. Errors are returned as a value of FAIL.
3031 * The return value is OK on success.
3032 * If OK is returned and len_change is not NULL, *len_change
3033 * is set to the change in the buffer length.
3034 */
3035 static int
3036SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3037{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003038 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003039 * There are three cases:
3040 * 1. NULL, or None - this is a deletion.
3041 * 2. A string - this is a replacement.
3042 * 3. Anything else - this is an error.
3043 */
3044 if (line == Py_None || line == NULL)
3045 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003046 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003047
3048 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003049 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003050
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003051 VimTryStart();
3052
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003053 if (u_savedel((linenr_T)n, 1L) == FAIL)
3054 PyErr_SetVim(_("cannot save undo information"));
3055 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3056 PyErr_SetVim(_("cannot delete line"));
3057 else
3058 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003059 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003060 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3061 deleted_lines_mark((linenr_T)n, 1L);
3062 }
3063
Bram Moolenaar105bc352013-05-17 16:03:57 +02003064 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003065
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003066 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003067 return FAIL;
3068
3069 if (len_change)
3070 *len_change = -1;
3071
3072 return OK;
3073 }
3074 else if (PyString_Check(line))
3075 {
3076 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003077 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003078
3079 if (save == NULL)
3080 return FAIL;
3081
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003082 VimTryStart();
3083
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003084 /* We do not need to free "save" if ml_replace() consumes it. */
3085 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003086 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003087
3088 if (u_savesub((linenr_T)n) == FAIL)
3089 {
3090 PyErr_SetVim(_("cannot save undo information"));
3091 vim_free(save);
3092 }
3093 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3094 {
3095 PyErr_SetVim(_("cannot replace line"));
3096 vim_free(save);
3097 }
3098 else
3099 changed_bytes((linenr_T)n, 0);
3100
Bram Moolenaar105bc352013-05-17 16:03:57 +02003101 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003102
3103 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003104 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003105 check_cursor_col();
3106
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003107 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003108 return FAIL;
3109
3110 if (len_change)
3111 *len_change = 0;
3112
3113 return OK;
3114 }
3115 else
3116 {
3117 PyErr_BadArgument();
3118 return FAIL;
3119 }
3120}
3121
Bram Moolenaar19e60942011-06-19 00:27:51 +02003122/* Replace a range of lines in the specified buffer. The line numbers are in
3123 * Vim format (1-based). The range is from lo up to, but not including, hi.
3124 * The replacement lines are given as a Python list of string objects. The
3125 * list is checked for validity and correct format. Errors are returned as a
3126 * value of FAIL. The return value is OK on success.
3127 * If OK is returned and len_change is not NULL, *len_change
3128 * is set to the change in the buffer length.
3129 */
3130 static int
3131SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3132{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003133 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003134 * There are three cases:
3135 * 1. NULL, or None - this is a deletion.
3136 * 2. A list - this is a replacement.
3137 * 3. Anything else - this is an error.
3138 */
3139 if (list == Py_None || list == NULL)
3140 {
3141 PyInt i;
3142 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003143 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003144
3145 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003146 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003147 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003148
3149 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3150 PyErr_SetVim(_("cannot save undo information"));
3151 else
3152 {
3153 for (i = 0; i < n; ++i)
3154 {
3155 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3156 {
3157 PyErr_SetVim(_("cannot delete line"));
3158 break;
3159 }
3160 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003161 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003162 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3163 deleted_lines_mark((linenr_T)lo, (long)i);
3164 }
3165
Bram Moolenaar105bc352013-05-17 16:03:57 +02003166 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003167
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003168 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003169 return FAIL;
3170
3171 if (len_change)
3172 *len_change = -n;
3173
3174 return OK;
3175 }
3176 else if (PyList_Check(list))
3177 {
3178 PyInt i;
3179 PyInt new_len = PyList_Size(list);
3180 PyInt old_len = hi - lo;
3181 PyInt extra = 0; /* lines added to text, can be negative */
3182 char **array;
3183 buf_T *savebuf;
3184
3185 if (new_len == 0) /* avoid allocating zero bytes */
3186 array = NULL;
3187 else
3188 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003189 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003190 if (array == NULL)
3191 {
3192 PyErr_NoMemory();
3193 return FAIL;
3194 }
3195 }
3196
3197 for (i = 0; i < new_len; ++i)
3198 {
3199 PyObject *line = PyList_GetItem(list, i);
3200
3201 array[i] = StringToLine(line);
3202 if (array[i] == NULL)
3203 {
3204 while (i)
3205 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003206 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003207 return FAIL;
3208 }
3209 }
3210
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003211 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003212 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003213
3214 // START of region without "return". Must call restore_buffer()!
3215 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003216
3217 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3218 PyErr_SetVim(_("cannot save undo information"));
3219
3220 /* If the size of the range is reducing (ie, new_len < old_len) we
3221 * need to delete some old_len. We do this at the start, by
3222 * repeatedly deleting line "lo".
3223 */
3224 if (!PyErr_Occurred())
3225 {
3226 for (i = 0; i < old_len - new_len; ++i)
3227 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3228 {
3229 PyErr_SetVim(_("cannot delete line"));
3230 break;
3231 }
3232 extra -= i;
3233 }
3234
3235 /* For as long as possible, replace the existing old_len with the
3236 * new old_len. This is a more efficient operation, as it requires
3237 * less memory allocation and freeing.
3238 */
3239 if (!PyErr_Occurred())
3240 {
3241 for (i = 0; i < old_len && i < new_len; ++i)
3242 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3243 == FAIL)
3244 {
3245 PyErr_SetVim(_("cannot replace line"));
3246 break;
3247 }
3248 }
3249 else
3250 i = 0;
3251
3252 /* Now we may need to insert the remaining new old_len. If we do, we
3253 * must free the strings as we finish with them (we can't pass the
3254 * responsibility to vim in this case).
3255 */
3256 if (!PyErr_Occurred())
3257 {
3258 while (i < new_len)
3259 {
3260 if (ml_append((linenr_T)(lo + i - 1),
3261 (char_u *)array[i], 0, FALSE) == FAIL)
3262 {
3263 PyErr_SetVim(_("cannot insert line"));
3264 break;
3265 }
3266 vim_free(array[i]);
3267 ++i;
3268 ++extra;
3269 }
3270 }
3271
3272 /* Free any left-over old_len, as a result of an error */
3273 while (i < new_len)
3274 {
3275 vim_free(array[i]);
3276 ++i;
3277 }
3278
3279 /* Free the array of old_len. All of its contents have now
3280 * been dealt with (either freed, or the responsibility passed
3281 * to vim.
3282 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003283 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003284
3285 /* Adjust marks. Invalidate any which lie in the
3286 * changed range, and move any in the remainder of the buffer.
3287 */
3288 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3289 (long)MAXLNUM, (long)extra);
3290 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3291
Bram Moolenaar105bc352013-05-17 16:03:57 +02003292 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003293 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3294
Bram Moolenaar105bc352013-05-17 16:03:57 +02003295 // END of region without "return".
3296 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003297
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003298 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003299 return FAIL;
3300
3301 if (len_change)
3302 *len_change = new_len - old_len;
3303
3304 return OK;
3305 }
3306 else
3307 {
3308 PyErr_BadArgument();
3309 return FAIL;
3310 }
3311}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003312
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003313/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003314 * The line number is in Vim format (1-based). The lines to be inserted are
3315 * given as a Python list of string objects or as a single string. The lines
3316 * to be added are checked for validity and correct format. Errors are
3317 * returned as a value of FAIL. The return value is OK on success.
3318 * If OK is returned and len_change is not NULL, *len_change
3319 * is set to the change in the buffer length.
3320 */
3321 static int
3322InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3323{
3324 /* First of all, we check the type of the supplied Python object.
3325 * It must be a string or a list, or the call is in error.
3326 */
3327 if (PyString_Check(lines))
3328 {
3329 char *str = StringToLine(lines);
3330 buf_T *savebuf;
3331
3332 if (str == NULL)
3333 return FAIL;
3334
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003335 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003336 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003337 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003338
3339 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3340 PyErr_SetVim(_("cannot save undo information"));
3341 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3342 PyErr_SetVim(_("cannot insert line"));
3343 else
3344 appended_lines_mark((linenr_T)n, 1L);
3345
3346 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003347 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 update_screen(VALID);
3349
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003350 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003351 return FAIL;
3352
3353 if (len_change)
3354 *len_change = 1;
3355
3356 return OK;
3357 }
3358 else if (PyList_Check(lines))
3359 {
3360 PyInt i;
3361 PyInt size = PyList_Size(lines);
3362 char **array;
3363 buf_T *savebuf;
3364
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003365 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003366 if (array == NULL)
3367 {
3368 PyErr_NoMemory();
3369 return FAIL;
3370 }
3371
3372 for (i = 0; i < size; ++i)
3373 {
3374 PyObject *line = PyList_GetItem(lines, i);
3375 array[i] = StringToLine(line);
3376
3377 if (array[i] == NULL)
3378 {
3379 while (i)
3380 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003381 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003382 return FAIL;
3383 }
3384 }
3385
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003386 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003387 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003388 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003389
3390 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3391 PyErr_SetVim(_("cannot save undo information"));
3392 else
3393 {
3394 for (i = 0; i < size; ++i)
3395 {
3396 if (ml_append((linenr_T)(n + i),
3397 (char_u *)array[i], 0, FALSE) == FAIL)
3398 {
3399 PyErr_SetVim(_("cannot insert line"));
3400
3401 /* Free the rest of the lines */
3402 while (i < size)
3403 vim_free(array[i++]);
3404
3405 break;
3406 }
3407 vim_free(array[i]);
3408 }
3409 if (i > 0)
3410 appended_lines_mark((linenr_T)n, (long)i);
3411 }
3412
3413 /* Free the array of lines. All of its contents have now
3414 * been freed.
3415 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003416 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003417
Bram Moolenaar105bc352013-05-17 16:03:57 +02003418 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003419 update_screen(VALID);
3420
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003421 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003422 return FAIL;
3423
3424 if (len_change)
3425 *len_change = size;
3426
3427 return OK;
3428 }
3429 else
3430 {
3431 PyErr_BadArgument();
3432 return FAIL;
3433 }
3434}
3435
3436/*
3437 * Common routines for buffers and line ranges
3438 * -------------------------------------------
3439 */
3440
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003441typedef struct
3442{
3443 PyObject_HEAD
3444 buf_T *buf;
3445} BufferObject;
3446
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003447 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003448CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003449{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003450 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003451 {
3452 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3453 return -1;
3454 }
3455
3456 return 0;
3457}
3458
3459 static PyObject *
3460RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3461{
3462 if (CheckBuffer(self))
3463 return NULL;
3464
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003465 if (end == -1)
3466 end = self->buf->b_ml.ml_line_count;
3467
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003468 if (n < 0)
3469 n += end - start + 1;
3470
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003471 if (n < 0 || n > end - start)
3472 {
3473 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3474 return NULL;
3475 }
3476
3477 return GetBufferLine(self->buf, n+start);
3478}
3479
3480 static PyObject *
3481RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3482{
3483 PyInt size;
3484
3485 if (CheckBuffer(self))
3486 return NULL;
3487
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003488 if (end == -1)
3489 end = self->buf->b_ml.ml_line_count;
3490
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003491 size = end - start + 1;
3492
3493 if (lo < 0)
3494 lo = 0;
3495 else if (lo > size)
3496 lo = size;
3497 if (hi < 0)
3498 hi = 0;
3499 if (hi < lo)
3500 hi = lo;
3501 else if (hi > size)
3502 hi = size;
3503
3504 return GetBufferLineList(self->buf, lo+start, hi+start);
3505}
3506
3507 static PyInt
3508RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3509{
3510 PyInt len_change;
3511
3512 if (CheckBuffer(self))
3513 return -1;
3514
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003515 if (end == -1)
3516 end = self->buf->b_ml.ml_line_count;
3517
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003518 if (n < 0)
3519 n += end - start + 1;
3520
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003521 if (n < 0 || n > end - start)
3522 {
3523 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3524 return -1;
3525 }
3526
3527 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3528 return -1;
3529
3530 if (new_end)
3531 *new_end = end + len_change;
3532
3533 return 0;
3534}
3535
Bram Moolenaar19e60942011-06-19 00:27:51 +02003536 static PyInt
3537RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3538{
3539 PyInt size;
3540 PyInt len_change;
3541
3542 /* Self must be a valid buffer */
3543 if (CheckBuffer(self))
3544 return -1;
3545
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003546 if (end == -1)
3547 end = self->buf->b_ml.ml_line_count;
3548
Bram Moolenaar19e60942011-06-19 00:27:51 +02003549 /* Sort out the slice range */
3550 size = end - start + 1;
3551
3552 if (lo < 0)
3553 lo = 0;
3554 else if (lo > size)
3555 lo = size;
3556 if (hi < 0)
3557 hi = 0;
3558 if (hi < lo)
3559 hi = lo;
3560 else if (hi > size)
3561 hi = size;
3562
3563 if (SetBufferLineList(self->buf, lo + start, hi + start,
3564 val, &len_change) == FAIL)
3565 return -1;
3566
3567 if (new_end)
3568 *new_end = end + len_change;
3569
3570 return 0;
3571}
3572
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003573
3574 static PyObject *
3575RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3576{
3577 PyObject *lines;
3578 PyInt len_change;
3579 PyInt max;
3580 PyInt n;
3581
3582 if (CheckBuffer(self))
3583 return NULL;
3584
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003585 if (end == -1)
3586 end = self->buf->b_ml.ml_line_count;
3587
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003588 max = n = end - start + 1;
3589
3590 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3591 return NULL;
3592
3593 if (n < 0 || n > max)
3594 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003595 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003596 return NULL;
3597 }
3598
3599 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3600 return NULL;
3601
3602 if (new_end)
3603 *new_end = end + len_change;
3604
3605 Py_INCREF(Py_None);
3606 return Py_None;
3607}
3608
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003609/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003610 */
3611
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003612static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003613static PySequenceMethods RangeAsSeq;
3614static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003615
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003616typedef struct
3617{
3618 PyObject_HEAD
3619 BufferObject *buf;
3620 PyInt start;
3621 PyInt end;
3622} RangeObject;
3623
3624 static PyObject *
3625RangeNew(buf_T *buf, PyInt start, PyInt end)
3626{
3627 BufferObject *bufr;
3628 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003629 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003630 if (self == NULL)
3631 return NULL;
3632
3633 bufr = (BufferObject *)BufferNew(buf);
3634 if (bufr == NULL)
3635 {
3636 Py_DECREF(self);
3637 return NULL;
3638 }
3639 Py_INCREF(bufr);
3640
3641 self->buf = bufr;
3642 self->start = start;
3643 self->end = end;
3644
3645 return (PyObject *)(self);
3646}
3647
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003648 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003649RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003650{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003651 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003652 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003653 PyObject_GC_Del((void *)(self));
3654}
3655
3656 static int
3657RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3658{
3659 Py_VISIT(((PyObject *)(self->buf)));
3660 return 0;
3661}
3662
3663 static int
3664RangeClear(RangeObject *self)
3665{
3666 Py_CLEAR(self->buf);
3667 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003668}
3669
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003670 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003671RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003672{
3673 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003674 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003675 return -1; /* ??? */
3676
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003678}
3679
3680 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003681RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003682{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003683 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003684}
3685
3686 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003687RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003688{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003689 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003690}
3691
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003692static char *RangeAttrs[] = {
3693 "start", "end",
3694 NULL
3695};
3696
3697 static PyObject *
3698RangeDir(PyObject *self)
3699{
3700 return ObjectDir(self, RangeAttrs);
3701}
3702
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003703 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003704RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003705{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003706 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003707}
3708
3709 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003710RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003711{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003712 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003713 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
3714 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003715 else
3716 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003717 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003718
3719 if (name == NULL)
3720 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003721
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003722 return PyString_FromFormat("<range %s (%d:%d)>",
3723 name, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003724 }
3725}
3726
3727static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003728 /* name, function, calling, documentation */
3729 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003730 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
3731 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003732};
3733
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003734static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003735static PySequenceMethods BufferAsSeq;
3736static PyMappingMethods BufferAsMapping;
3737
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003738 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02003739BufferNew(buf_T *buf)
3740{
3741 /* We need to handle deletion of buffers underneath us.
3742 * If we add a "b_python*_ref" field to the buf_T structure,
3743 * then we can get at it in buf_freeall() in vim. We then
3744 * need to create only ONE Python object per buffer - if
3745 * we try to create a second, just INCREF the existing one
3746 * and return it. The (single) Python object referring to
3747 * the buffer is stored in "b_python*_ref".
3748 * Question: what to do on a buf_freeall(). We'll probably
3749 * have to either delete the Python object (DECREF it to
3750 * zero - a bad idea, as it leaves dangling refs!) or
3751 * set the buf_T * value to an invalid value (-1?), which
3752 * means we need checks in all access functions... Bah.
3753 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003754 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003755 * b_python_ref and b_python3_ref fields respectively.
3756 */
3757
3758 BufferObject *self;
3759
3760 if (BUF_PYTHON_REF(buf) != NULL)
3761 {
3762 self = BUF_PYTHON_REF(buf);
3763 Py_INCREF(self);
3764 }
3765 else
3766 {
3767 self = PyObject_NEW(BufferObject, &BufferType);
3768 if (self == NULL)
3769 return NULL;
3770 self->buf = buf;
3771 BUF_PYTHON_REF(buf) = self;
3772 }
3773
3774 return (PyObject *)(self);
3775}
3776
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003777 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003778BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003779{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003780 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
3781 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003782
3783 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003784}
3785
Bram Moolenaar971db462013-05-12 18:44:48 +02003786 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003787BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02003788{
3789 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003790 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02003791 return -1; /* ??? */
3792
Bram Moolenaard6e39182013-05-21 18:30:34 +02003793 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02003794}
3795
3796 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003797BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02003798{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003799 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003800}
3801
3802 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003803BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02003804{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003805 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003806}
3807
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003808static char *BufferAttrs[] = {
3809 "name", "number", "vars", "options", "valid",
3810 NULL
3811};
3812
3813 static PyObject *
3814BufferDir(PyObject *self)
3815{
3816 return ObjectDir(self, BufferAttrs);
3817}
3818
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003819 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003820BufferAttrValid(BufferObject *self, char *name)
3821{
3822 PyObject *r;
3823
3824 if (strcmp(name, "valid") != 0)
3825 return NULL;
3826
3827 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
3828 Py_INCREF(r);
3829 return r;
3830}
3831
3832 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003833BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003834{
3835 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02003836 return PyString_FromString((self->buf->b_ffname == NULL
3837 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003838 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003840 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003841 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003842 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003843 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
3844 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003845 else if (strcmp(name, "__members__") == 0)
3846 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003847 else
3848 return NULL;
3849}
3850
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003851 static int
3852BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
3853{
3854 if (CheckBuffer(self))
3855 return -1;
3856
3857 if (strcmp(name, "name") == 0)
3858 {
3859 char_u *val;
3860 aco_save_T aco;
3861 int r;
3862 PyObject *todecref;
3863
3864 if (!(val = StringToChars(valObject, &todecref)))
3865 return -1;
3866
3867 VimTryStart();
3868 /* Using aucmd_*: autocommands will be executed by rename_buffer */
3869 aucmd_prepbuf(&aco, self->buf);
3870 r = rename_buffer(val);
3871 aucmd_restbuf(&aco);
3872 Py_XDECREF(todecref);
3873 if (VimTryEnd())
3874 return -1;
3875
3876 if (r == FAIL)
3877 {
3878 PyErr_SetVim(_("failed to rename buffer"));
3879 return -1;
3880 }
3881 return 0;
3882 }
3883 else
3884 {
3885 PyErr_SetString(PyExc_AttributeError, name);
3886 return -1;
3887 }
3888}
3889
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003892{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003894}
3895
3896 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898{
3899 pos_T *posp;
3900 char *pmark;
3901 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003902 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003903
Bram Moolenaard6e39182013-05-21 18:30:34 +02003904 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003905 return NULL;
3906
3907 if (!PyArg_ParseTuple(args, "s", &pmark))
3908 return NULL;
3909 mark = *pmark;
3910
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003911 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02003912 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003914 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003915 if (VimTryEnd())
3916 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003917
3918 if (posp == NULL)
3919 {
3920 PyErr_SetVim(_("invalid mark name"));
3921 return NULL;
3922 }
3923
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003924 if (posp->lnum <= 0)
3925 {
3926 /* Or raise an error? */
3927 Py_INCREF(Py_None);
3928 return Py_None;
3929 }
3930
3931 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3932}
3933
3934 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003935BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003936{
3937 PyInt start;
3938 PyInt end;
3939
Bram Moolenaard6e39182013-05-21 18:30:34 +02003940 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003941 return NULL;
3942
3943 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3944 return NULL;
3945
Bram Moolenaard6e39182013-05-21 18:30:34 +02003946 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003947}
3948
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003949 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003950BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003951{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003952 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003953 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003954 else
3955 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003956 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003957
3958 if (name == NULL)
3959 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003960
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003961 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003962 }
3963}
3964
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003965static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02003966 /* name, function, calling, documentation */
3967 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
3968 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
3969 {"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 +02003970 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
3971 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972};
3973
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003974/*
3975 * Buffer list object - Implementation
3976 */
3977
3978static PyTypeObject BufMapType;
3979
3980typedef struct
3981{
3982 PyObject_HEAD
3983} BufMapObject;
3984
3985 static PyInt
3986BufMapLength(PyObject *self UNUSED)
3987{
3988 buf_T *b = firstbuf;
3989 PyInt n = 0;
3990
3991 while (b)
3992 {
3993 ++n;
3994 b = b->b_next;
3995 }
3996
3997 return n;
3998}
3999
4000 static PyObject *
4001BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4002{
4003 buf_T *b;
4004 int bnr;
4005
4006#if PY_MAJOR_VERSION < 3
4007 if (PyInt_Check(keyObject))
4008 bnr = PyInt_AsLong(keyObject);
4009 else
4010#endif
4011 if (PyLong_Check(keyObject))
4012 bnr = PyLong_AsLong(keyObject);
4013 else
4014 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004015 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004016 return NULL;
4017 }
4018
4019 b = buflist_findnr(bnr);
4020
4021 if (b)
4022 return BufferNew(b);
4023 else
4024 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004025 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004026 return NULL;
4027 }
4028}
4029
4030 static void
4031BufMapIterDestruct(PyObject *buffer)
4032{
4033 /* Iteration was stopped before all buffers were processed */
4034 if (buffer)
4035 {
4036 Py_DECREF(buffer);
4037 }
4038}
4039
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004040 static int
4041BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4042{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004043 if (buffer)
4044 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004045 return 0;
4046}
4047
4048 static int
4049BufMapIterClear(PyObject **buffer)
4050{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004051 if (*buffer)
4052 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004053 return 0;
4054}
4055
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004056 static PyObject *
4057BufMapIterNext(PyObject **buffer)
4058{
4059 PyObject *next;
4060 PyObject *r;
4061
4062 if (!*buffer)
4063 return NULL;
4064
4065 r = *buffer;
4066
4067 if (CheckBuffer((BufferObject *)(r)))
4068 {
4069 *buffer = NULL;
4070 return NULL;
4071 }
4072
4073 if (!((BufferObject *)(r))->buf->b_next)
4074 next = NULL;
4075 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4076 return NULL;
4077 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004078 /* Do not increment reference: we no longer hold it (decref), but whoever
4079 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004080 return r;
4081}
4082
4083 static PyObject *
4084BufMapIter(PyObject *self UNUSED)
4085{
4086 PyObject *buffer;
4087
4088 buffer = BufferNew(firstbuf);
4089 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004090 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4091 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004092}
4093
4094static PyMappingMethods BufMapAsMapping = {
4095 (lenfunc) BufMapLength,
4096 (binaryfunc) BufMapItem,
4097 (objobjargproc) 0,
4098};
4099
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004100/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 */
4102
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004103static char *CurrentAttrs[] = {
4104 "buffer", "window", "line", "range", "tabpage",
4105 NULL
4106};
4107
4108 static PyObject *
4109CurrentDir(PyObject *self)
4110{
4111 return ObjectDir(self, CurrentAttrs);
4112}
4113
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004114 static PyObject *
4115CurrentGetattr(PyObject *self UNUSED, char *name)
4116{
4117 if (strcmp(name, "buffer") == 0)
4118 return (PyObject *)BufferNew(curbuf);
4119 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004120 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004121 else if (strcmp(name, "tabpage") == 0)
4122 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004123 else if (strcmp(name, "line") == 0)
4124 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4125 else if (strcmp(name, "range") == 0)
4126 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004127 else if (strcmp(name, "__members__") == 0)
4128 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004129 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004130#if PY_MAJOR_VERSION < 3
4131 return Py_FindMethod(WindowMethods, self, name);
4132#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004133 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004134#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004135}
4136
4137 static int
4138CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4139{
4140 if (strcmp(name, "line") == 0)
4141 {
4142 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4143 return -1;
4144
4145 return 0;
4146 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004147 else if (strcmp(name, "buffer") == 0)
4148 {
4149 int count;
4150
4151 if (value->ob_type != &BufferType)
4152 {
4153 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
4154 return -1;
4155 }
4156
4157 if (CheckBuffer((BufferObject *)(value)))
4158 return -1;
4159 count = ((BufferObject *)(value))->buf->b_fnum;
4160
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004161 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004162 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4163 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004164 if (VimTryEnd())
4165 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004166 PyErr_SetVim(_("failed to switch to given buffer"));
4167 return -1;
4168 }
4169
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004170 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004171 }
4172 else if (strcmp(name, "window") == 0)
4173 {
4174 int count;
4175
4176 if (value->ob_type != &WindowType)
4177 {
4178 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
4179 return -1;
4180 }
4181
4182 if (CheckWindow((WindowObject *)(value)))
4183 return -1;
4184 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4185
4186 if (!count)
4187 {
4188 PyErr_SetString(PyExc_ValueError,
4189 _("failed to find window in the current tab page"));
4190 return -1;
4191 }
4192
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004193 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004194 win_goto(((WindowObject *)(value))->win);
4195 if (((WindowObject *)(value))->win != curwin)
4196 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004197 if (VimTryEnd())
4198 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004199 PyErr_SetString(PyExc_RuntimeError,
4200 _("did not switch to the specified window"));
4201 return -1;
4202 }
4203
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004204 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004205 }
4206 else if (strcmp(name, "tabpage") == 0)
4207 {
4208 if (value->ob_type != &TabPageType)
4209 {
4210 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
4211 return -1;
4212 }
4213
4214 if (CheckTabPage((TabPageObject *)(value)))
4215 return -1;
4216
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004217 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004218 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4219 if (((TabPageObject *)(value))->tab != curtab)
4220 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004221 if (VimTryEnd())
4222 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004223 PyErr_SetString(PyExc_RuntimeError,
4224 _("did not switch to the specified tab page"));
4225 return -1;
4226 }
4227
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004228 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004229 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004230 else
4231 {
4232 PyErr_SetString(PyExc_AttributeError, name);
4233 return -1;
4234 }
4235}
4236
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004237static struct PyMethodDef CurrentMethods[] = {
4238 /* name, function, calling, documentation */
4239 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4240 { NULL, NULL, 0, NULL}
4241};
4242
Bram Moolenaardb913952012-06-29 12:54:53 +02004243 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004244init_range_cmd(exarg_T *eap)
4245{
4246 RangeStart = eap->line1;
4247 RangeEnd = eap->line2;
4248}
4249
4250 static void
4251init_range_eval(typval_T *rettv UNUSED)
4252{
4253 RangeStart = (PyInt) curwin->w_cursor.lnum;
4254 RangeEnd = RangeStart;
4255}
4256
4257 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004258run_cmd(const char *cmd, void *arg UNUSED
4259#ifdef PY_CAN_RECURSE
4260 , PyGILState_STATE *pygilstate UNUSED
4261#endif
4262 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004263{
4264 PyRun_SimpleString((char *) cmd);
4265}
4266
4267static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4268static int code_hdr_len = 30;
4269
4270 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004271run_do(const char *cmd, void *arg UNUSED
4272#ifdef PY_CAN_RECURSE
4273 , PyGILState_STATE *pygilstate
4274#endif
4275 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004276{
4277 PyInt lnum;
4278 size_t len;
4279 char *code;
4280 int status;
4281 PyObject *pyfunc, *pymain;
4282
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004283 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004284 {
4285 EMSG(_("cannot save undo information"));
4286 return;
4287 }
4288
4289 len = code_hdr_len + STRLEN(cmd);
4290 code = PyMem_New(char, len + 1);
4291 memcpy(code, code_hdr, code_hdr_len);
4292 STRCPY(code + code_hdr_len, cmd);
4293 status = PyRun_SimpleString(code);
4294 PyMem_Free(code);
4295
4296 if (status)
4297 {
4298 EMSG(_("failed to run the code"));
4299 return;
4300 }
4301
4302 status = 0;
4303 pymain = PyImport_AddModule("__main__");
4304 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004305#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004306 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004307#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004308
4309 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4310 {
4311 PyObject *line, *linenr, *ret;
4312
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004313#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004314 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004315#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004316 if (!(line = GetBufferLine(curbuf, lnum)))
4317 goto err;
4318 if (!(linenr = PyInt_FromLong((long) lnum)))
4319 {
4320 Py_DECREF(line);
4321 goto err;
4322 }
4323 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4324 Py_DECREF(line);
4325 Py_DECREF(linenr);
4326 if (!ret)
4327 goto err;
4328
4329 if (ret != Py_None)
4330 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4331 goto err;
4332
4333 Py_XDECREF(ret);
4334 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004335#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004336 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004337#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004338 }
4339 goto out;
4340err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004341#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004342 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004343#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004344 PyErr_PrintEx(0);
4345 PythonIO_Flush();
4346 status = 1;
4347out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004348#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004349 if (!status)
4350 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004351#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004352 Py_DECREF(pyfunc);
4353 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4354 if (status)
4355 return;
4356 check_cursor();
4357 update_curbuf(NOT_VALID);
4358}
4359
4360 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004361run_eval(const char *cmd, typval_T *rettv
4362#ifdef PY_CAN_RECURSE
4363 , PyGILState_STATE *pygilstate UNUSED
4364#endif
4365 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004366{
4367 PyObject *r;
4368
4369 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4370 if (r == NULL)
4371 {
4372 if (PyErr_Occurred() && !msg_silent)
4373 PyErr_PrintEx(0);
4374 EMSG(_("E858: Eval did not return a valid python object"));
4375 }
4376 else
4377 {
4378 if (ConvertFromPyObject(r, rettv) == -1)
4379 EMSG(_("E859: Failed to convert returned python object to vim value"));
4380 Py_DECREF(r);
4381 }
4382 PyErr_Clear();
4383}
4384
4385 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004386set_ref_in_py(const int copyID)
4387{
4388 pylinkedlist_T *cur;
4389 dict_T *dd;
4390 list_T *ll;
4391
4392 if (lastdict != NULL)
4393 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4394 {
4395 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4396 if (dd->dv_copyID != copyID)
4397 {
4398 dd->dv_copyID = copyID;
4399 set_ref_in_ht(&dd->dv_hashtab, copyID);
4400 }
4401 }
4402
4403 if (lastlist != NULL)
4404 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4405 {
4406 ll = ((ListObject *) (cur->pll_obj))->list;
4407 if (ll->lv_copyID != copyID)
4408 {
4409 ll->lv_copyID = copyID;
4410 set_ref_in_list(ll, copyID);
4411 }
4412 }
4413}
4414
4415 static int
4416set_string_copy(char_u *str, typval_T *tv)
4417{
4418 tv->vval.v_string = vim_strsave(str);
4419 if (tv->vval.v_string == NULL)
4420 {
4421 PyErr_NoMemory();
4422 return -1;
4423 }
4424 return 0;
4425}
4426
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004427 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004428pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004429{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004430 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004431 char_u *key;
4432 dictitem_T *di;
4433 PyObject *keyObject;
4434 PyObject *valObject;
4435 Py_ssize_t iter = 0;
4436
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004437 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004438 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004439
4440 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004441 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004442
4443 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4444 {
4445 DICTKEY_DECL
4446
Bram Moolenaara03e6312013-05-29 22:49:26 +02004447 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004448 {
4449 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004450 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004451 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004452
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004453 if (!DICTKEY_SET_KEY)
4454 {
4455 dict_unref(dict);
4456 return -1;
4457 }
4458 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004459
4460 di = dictitem_alloc(key);
4461
4462 DICTKEY_UNREF
4463
4464 if (di == NULL)
4465 {
4466 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004467 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004468 return -1;
4469 }
4470 di->di_tv.v_lock = 0;
4471
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004472 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004473 {
4474 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004475 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004476 return -1;
4477 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004478
4479 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004480 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004481 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004482 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004483 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004484 PyErr_SetVim(_("failed to add key to dictionary"));
4485 return -1;
4486 }
4487 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004488
4489 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004490 return 0;
4491}
4492
4493 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004494pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004495{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004496 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004497 char_u *key;
4498 dictitem_T *di;
4499 PyObject *list;
4500 PyObject *litem;
4501 PyObject *keyObject;
4502 PyObject *valObject;
4503 Py_ssize_t lsize;
4504
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004505 if (!(dict = dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004506 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004507
4508 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004509 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004510
4511 list = PyMapping_Items(obj);
4512 if (list == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004513 {
4514 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004515 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004516 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004517 lsize = PyList_Size(list);
4518 while (lsize--)
4519 {
4520 DICTKEY_DECL
4521
4522 litem = PyList_GetItem(list, lsize);
4523 if (litem == NULL)
4524 {
4525 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004526 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004527 return -1;
4528 }
4529
Bram Moolenaara03e6312013-05-29 22:49:26 +02004530 if (!(keyObject = PyTuple_GetItem(litem, 0)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004531 {
4532 Py_DECREF(list);
4533 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004534 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004535 return -1;
4536 }
4537
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004538 if (!DICTKEY_SET_KEY)
4539 {
4540 dict_unref(dict);
4541 Py_DECREF(list);
4542 Py_DECREF(litem);
4543 DICTKEY_UNREF
4544 return -1;
4545 }
4546 DICTKEY_CHECK_EMPTY(-1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004547
Bram Moolenaara03e6312013-05-29 22:49:26 +02004548 if (!(valObject = PyTuple_GetItem(litem, 1)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004549 {
4550 Py_DECREF(list);
4551 Py_DECREF(litem);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004552 dict_unref(dict);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004553 DICTKEY_UNREF
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004554 return -1;
4555 }
4556
Bram Moolenaara03e6312013-05-29 22:49:26 +02004557 Py_DECREF(litem);
4558
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004559 di = dictitem_alloc(key);
4560
4561 DICTKEY_UNREF
4562
4563 if (di == NULL)
4564 {
4565 Py_DECREF(list);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004566 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004567 PyErr_NoMemory();
4568 return -1;
4569 }
4570 di->di_tv.v_lock = 0;
4571
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004572 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004573 {
4574 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004575 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004576 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004577 return -1;
4578 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004579
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004580 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004581 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004582 dictitem_free(di);
4583 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004584 Py_DECREF(list);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004585 PyErr_SetVim(_("failed to add key to dictionary"));
4586 return -1;
4587 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004588 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004589 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004590 Py_DECREF(list);
4591 return 0;
4592}
4593
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004594 static list_T *
4595py_list_alloc()
4596{
4597 list_T *r;
4598
4599 if (!(r = list_alloc()))
4600 {
4601 PyErr_NoMemory();
4602 return NULL;
4603 }
4604 ++r->lv_refcount;
4605
4606 return r;
4607}
4608
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004609 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004610pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004611{
4612 list_T *l;
4613
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004614 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004615 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004616
4617 tv->v_type = VAR_LIST;
4618 tv->vval.v_list = l;
4619
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004620 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004621 {
4622 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004623 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004624 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004625
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004626 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004627 return 0;
4628}
4629
4630 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004631pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004632{
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004633 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004634 PyObject *item;
4635 list_T *l;
4636 listitem_T *li;
4637
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004638 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004639 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004640
4641 tv->vval.v_list = l;
4642 tv->v_type = VAR_LIST;
4643
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004644 if (!(iterator = PyObject_GetIter(obj)))
4645 {
4646 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004647 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004648 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004649
Bram Moolenaarc8366792013-05-29 22:46:26 +02004650 while ((item = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004651 {
4652 li = listitem_alloc();
4653 if (li == NULL)
4654 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004655 list_unref(l);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004656 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004657 PyErr_NoMemory();
4658 return -1;
4659 }
4660 li->li_tv.v_lock = 0;
4661
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004662 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
Bram Moolenaara03e6312013-05-29 22:49:26 +02004663 {
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004664 list_unref(l);
4665 listitem_free(li);
Bram Moolenaara03e6312013-05-29 22:49:26 +02004666 Py_DECREF(item);
4667 Py_DECREF(iterator);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004668 return -1;
Bram Moolenaara03e6312013-05-29 22:49:26 +02004669 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004670
4671 list_append(l, li);
4672
4673 Py_DECREF(item);
4674 }
4675
4676 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004677
4678 /* Iterator may have finished due to an exception */
4679 if (PyErr_Occurred())
4680 {
4681 list_unref(l);
4682 return -1;
4683 }
4684
4685 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004686 return 0;
4687}
4688
Bram Moolenaardb913952012-06-29 12:54:53 +02004689typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4690
4691 static int
4692convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004693 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004694{
4695 PyObject *capsule;
4696 char hexBuf[sizeof(void *) * 2 + 3];
4697
4698 sprintf(hexBuf, "%p", obj);
4699
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004700# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004701 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004702# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004703 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004704# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004705 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004706 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004707# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004708 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004709# else
4710 capsule = PyCObject_FromVoidPtr(tv, NULL);
4711# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004712 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4713 {
4714 Py_DECREF(capsule);
4715 tv->v_type = VAR_UNKNOWN;
4716 return -1;
4717 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004718 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004719 {
4720 tv->v_type = VAR_UNKNOWN;
4721 return -1;
4722 }
4723 /* As we are not using copy_tv which increments reference count we must
4724 * do it ourself. */
4725 switch(tv->v_type)
4726 {
4727 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4728 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4729 }
4730 }
4731 else
4732 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004733 typval_T *v;
4734
4735# ifdef PY_USE_CAPSULE
4736 v = PyCapsule_GetPointer(capsule, NULL);
4737# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02004738 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004739# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02004740 copy_tv(v, tv);
4741 }
4742 return 0;
4743}
4744
4745 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02004746ConvertFromPyMapping(PyObject *obj, typval_T *tv)
4747{
4748 PyObject *lookup_dict;
4749 int r;
4750
4751 if (!(lookup_dict = PyDict_New()))
4752 return -1;
4753
4754 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
4755 {
4756 tv->v_type = VAR_DICT;
4757 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4758 ++tv->vval.v_dict->dv_refcount;
4759 r = 0;
4760 }
4761 else if (PyDict_Check(obj))
4762 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
4763 else if (PyMapping_Check(obj))
4764 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
4765 else
4766 {
4767 PyErr_SetString(PyExc_TypeError,
4768 _("unable to convert object to vim dictionary"));
4769 r = -1;
4770 }
4771 Py_DECREF(lookup_dict);
4772 return r;
4773}
4774
4775 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02004776ConvertFromPyObject(PyObject *obj, typval_T *tv)
4777{
4778 PyObject *lookup_dict;
4779 int r;
4780
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004781 if (!(lookup_dict = PyDict_New()))
4782 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004783 r = _ConvertFromPyObject(obj, tv, lookup_dict);
4784 Py_DECREF(lookup_dict);
4785 return r;
4786}
4787
4788 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004789_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004790{
Bram Moolenaara9922d62013-05-30 13:01:18 +02004791 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02004792 {
4793 tv->v_type = VAR_DICT;
4794 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
4795 ++tv->vval.v_dict->dv_refcount;
4796 }
4797 else if (obj->ob_type == &ListType)
4798 {
4799 tv->v_type = VAR_LIST;
4800 tv->vval.v_list = (((ListObject *)(obj))->list);
4801 ++tv->vval.v_list->lv_refcount;
4802 }
4803 else if (obj->ob_type == &FunctionType)
4804 {
4805 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
4806 return -1;
4807
4808 tv->v_type = VAR_FUNC;
4809 func_ref(tv->vval.v_string);
4810 }
Bram Moolenaardb913952012-06-29 12:54:53 +02004811 else if (PyBytes_Check(obj))
4812 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004813 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02004814
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004815 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
4816 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004817 if (result == NULL)
4818 return -1;
4819
4820 if (set_string_copy(result, tv) == -1)
4821 return -1;
4822
4823 tv->v_type = VAR_STRING;
4824 }
4825 else if (PyUnicode_Check(obj))
4826 {
4827 PyObject *bytes;
4828 char_u *result;
4829
Bram Moolenaardb913952012-06-29 12:54:53 +02004830 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
4831 if (bytes == NULL)
4832 return -1;
4833
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02004834 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
4835 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02004836 if (result == NULL)
4837 return -1;
4838
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004839 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02004840 {
4841 Py_XDECREF(bytes);
4842 return -1;
4843 }
4844 Py_XDECREF(bytes);
4845
4846 tv->v_type = VAR_STRING;
4847 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02004848#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02004849 else if (PyInt_Check(obj))
4850 {
4851 tv->v_type = VAR_NUMBER;
4852 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
4853 }
4854#endif
4855 else if (PyLong_Check(obj))
4856 {
4857 tv->v_type = VAR_NUMBER;
4858 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
4859 }
4860 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004861 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004862#ifdef FEAT_FLOAT
4863 else if (PyFloat_Check(obj))
4864 {
4865 tv->v_type = VAR_FLOAT;
4866 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
4867 }
4868#endif
4869 else if (PyIter_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004870 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004871 else if (PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004872 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004873 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004874 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004875 else
4876 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004877 PyErr_SetString(PyExc_TypeError,
4878 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02004879 return -1;
4880 }
4881 return 0;
4882}
4883
4884 static PyObject *
4885ConvertToPyObject(typval_T *tv)
4886{
4887 if (tv == NULL)
4888 {
4889 PyErr_SetVim(_("NULL reference passed"));
4890 return NULL;
4891 }
4892 switch (tv->v_type)
4893 {
4894 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004895 return PyBytes_FromString(tv->vval.v_string == NULL
4896 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004897 case VAR_NUMBER:
4898 return PyLong_FromLong((long) tv->vval.v_number);
4899#ifdef FEAT_FLOAT
4900 case VAR_FLOAT:
4901 return PyFloat_FromDouble((double) tv->vval.v_float);
4902#endif
4903 case VAR_LIST:
4904 return ListNew(tv->vval.v_list);
4905 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02004906 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02004907 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02004908 return FunctionNew(tv->vval.v_string == NULL
4909 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02004910 case VAR_UNKNOWN:
4911 Py_INCREF(Py_None);
4912 return Py_None;
4913 default:
4914 PyErr_SetVim(_("internal error: invalid value type"));
4915 return NULL;
4916 }
4917}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004918
4919typedef struct
4920{
4921 PyObject_HEAD
4922} CurrentObject;
4923static PyTypeObject CurrentType;
4924
4925 static void
4926init_structs(void)
4927{
4928 vim_memset(&OutputType, 0, sizeof(OutputType));
4929 OutputType.tp_name = "vim.message";
4930 OutputType.tp_basicsize = sizeof(OutputObject);
4931 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
4932 OutputType.tp_doc = "vim message object";
4933 OutputType.tp_methods = OutputMethods;
4934#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004935 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
4936 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004937 OutputType.tp_alloc = call_PyType_GenericAlloc;
4938 OutputType.tp_new = call_PyType_GenericNew;
4939 OutputType.tp_free = call_PyObject_Free;
4940#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004941 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
4942 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004943#endif
4944
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004945 vim_memset(&IterType, 0, sizeof(IterType));
4946 IterType.tp_name = "vim.iter";
4947 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02004948 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004949 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02004950 IterType.tp_iter = (getiterfunc)IterIter;
4951 IterType.tp_iternext = (iternextfunc)IterNext;
4952 IterType.tp_dealloc = (destructor)IterDestructor;
4953 IterType.tp_traverse = (traverseproc)IterTraverse;
4954 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004955
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004956 vim_memset(&BufferType, 0, sizeof(BufferType));
4957 BufferType.tp_name = "vim.buffer";
4958 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004959 BufferType.tp_dealloc = (destructor)BufferDestructor;
4960 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004961 BufferType.tp_as_sequence = &BufferAsSeq;
4962 BufferType.tp_as_mapping = &BufferAsMapping;
4963 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
4964 BufferType.tp_doc = "vim buffer object";
4965 BufferType.tp_methods = BufferMethods;
4966#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004967 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004968 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004969 BufferType.tp_alloc = call_PyType_GenericAlloc;
4970 BufferType.tp_new = call_PyType_GenericNew;
4971 BufferType.tp_free = call_PyObject_Free;
4972#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004973 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004974 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004975#endif
4976
4977 vim_memset(&WindowType, 0, sizeof(WindowType));
4978 WindowType.tp_name = "vim.window";
4979 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02004980 WindowType.tp_dealloc = (destructor)WindowDestructor;
4981 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02004982 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004983 WindowType.tp_doc = "vim Window object";
4984 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004985 WindowType.tp_traverse = (traverseproc)WindowTraverse;
4986 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004987#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02004988 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
4989 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004990 WindowType.tp_alloc = call_PyType_GenericAlloc;
4991 WindowType.tp_new = call_PyType_GenericNew;
4992 WindowType.tp_free = call_PyObject_Free;
4993#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004994 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
4995 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004996#endif
4997
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004998 vim_memset(&TabPageType, 0, sizeof(TabPageType));
4999 TabPageType.tp_name = "vim.tabpage";
5000 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005001 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5002 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005003 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5004 TabPageType.tp_doc = "vim tab page object";
5005 TabPageType.tp_methods = TabPageMethods;
5006#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005007 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005008 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5009 TabPageType.tp_new = call_PyType_GenericNew;
5010 TabPageType.tp_free = call_PyObject_Free;
5011#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005013#endif
5014
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005015 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5016 BufMapType.tp_name = "vim.bufferlist";
5017 BufMapType.tp_basicsize = sizeof(BufMapObject);
5018 BufMapType.tp_as_mapping = &BufMapAsMapping;
5019 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005020 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005021 BufferType.tp_doc = "vim buffer list";
5022
5023 vim_memset(&WinListType, 0, sizeof(WinListType));
5024 WinListType.tp_name = "vim.windowlist";
5025 WinListType.tp_basicsize = sizeof(WinListType);
5026 WinListType.tp_as_sequence = &WinListAsSeq;
5027 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5028 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005029 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005030
5031 vim_memset(&TabListType, 0, sizeof(TabListType));
5032 TabListType.tp_name = "vim.tabpagelist";
5033 TabListType.tp_basicsize = sizeof(TabListType);
5034 TabListType.tp_as_sequence = &TabListAsSeq;
5035 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5036 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005037
5038 vim_memset(&RangeType, 0, sizeof(RangeType));
5039 RangeType.tp_name = "vim.range";
5040 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005041 RangeType.tp_dealloc = (destructor)RangeDestructor;
5042 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005043 RangeType.tp_as_sequence = &RangeAsSeq;
5044 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005045 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005046 RangeType.tp_doc = "vim Range object";
5047 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005048 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5049 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005050#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005051 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005052 RangeType.tp_alloc = call_PyType_GenericAlloc;
5053 RangeType.tp_new = call_PyType_GenericNew;
5054 RangeType.tp_free = call_PyObject_Free;
5055#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005057#endif
5058
5059 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5060 CurrentType.tp_name = "vim.currentdata";
5061 CurrentType.tp_basicsize = sizeof(CurrentObject);
5062 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5063 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005064 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5067 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005068#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5070 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005071#endif
5072
5073 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5074 DictionaryType.tp_name = "vim.dictionary";
5075 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005077 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005078 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005079 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005080 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5081 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005082 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5083 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5084 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005085#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5087 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005088#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5090 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005091#endif
5092
5093 vim_memset(&ListType, 0, sizeof(ListType));
5094 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005096 ListType.tp_basicsize = sizeof(ListObject);
5097 ListType.tp_as_sequence = &ListAsSeq;
5098 ListType.tp_as_mapping = &ListAsMapping;
5099 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
5100 ListType.tp_doc = "list pushing modifications to vim structure";
5101 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005103#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 ListType.tp_getattro = (getattrofunc)ListGetattro;
5105 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005106#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005107 ListType.tp_getattr = (getattrfunc)ListGetattr;
5108 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005109#endif
5110
5111 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005112 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005113 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005114 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5115 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005116 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
5117 FunctionType.tp_doc = "object that calls vim function";
5118 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005119 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005122#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005123 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124#endif
5125
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5127 OptionsType.tp_name = "vim.options";
5128 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005129 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 OptionsType.tp_doc = "object for manipulating options";
5131 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005132 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5133 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5134 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005135
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005136#if PY_MAJOR_VERSION >= 3
5137 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5138 vimmodule.m_name = "vim";
5139 vimmodule.m_doc = "Vim Python interface\n";
5140 vimmodule.m_size = -1;
5141 vimmodule.m_methods = VimMethods;
5142#endif
5143}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005144
5145#define PYTYPE_READY(type) \
5146 if (PyType_Ready(&type)) \
5147 return -1;
5148
5149 static int
5150init_types()
5151{
5152 PYTYPE_READY(IterType);
5153 PYTYPE_READY(BufferType);
5154 PYTYPE_READY(RangeType);
5155 PYTYPE_READY(WindowType);
5156 PYTYPE_READY(TabPageType);
5157 PYTYPE_READY(BufMapType);
5158 PYTYPE_READY(WinListType);
5159 PYTYPE_READY(TabListType);
5160 PYTYPE_READY(CurrentType);
5161 PYTYPE_READY(DictionaryType);
5162 PYTYPE_READY(ListType);
5163 PYTYPE_READY(FunctionType);
5164 PYTYPE_READY(OptionsType);
5165 PYTYPE_READY(OutputType);
5166 return 0;
5167}
5168
5169static BufMapObject TheBufferMap =
5170{
5171 PyObject_HEAD_INIT(&BufMapType)
5172};
5173
5174static WinListObject TheWindowList =
5175{
5176 PyObject_HEAD_INIT(&WinListType)
5177 NULL
5178};
5179
5180static CurrentObject TheCurrent =
5181{
5182 PyObject_HEAD_INIT(&CurrentType)
5183};
5184
5185static TabListObject TheTabPageList =
5186{
5187 PyObject_HEAD_INIT(&TabListType)
5188};
5189
5190static struct numeric_constant {
5191 char *name;
5192 int value;
5193} numeric_constants[] = {
5194 {"VAR_LOCKED", VAR_LOCKED},
5195 {"VAR_FIXED", VAR_FIXED},
5196 {"VAR_SCOPE", VAR_SCOPE},
5197 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5198};
5199
5200static struct object_constant {
5201 char *name;
5202 PyObject *value;
5203} object_constants[] = {
5204 {"buffers", (PyObject *)(void *)&TheBufferMap},
5205 {"windows", (PyObject *)(void *)&TheWindowList},
5206 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5207 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005208
5209 {"Buffer", (PyObject *)&BufferType},
5210 {"Range", (PyObject *)&RangeType},
5211 {"Window", (PyObject *)&WindowType},
5212 {"TabPage", (PyObject *)&TabPageType},
5213 {"Dictionary", (PyObject *)&DictionaryType},
5214 {"List", (PyObject *)&ListType},
5215 {"Function", (PyObject *)&FunctionType},
5216 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005217};
5218
5219typedef int (*object_adder)(PyObject *, const char *, PyObject *);
5220
5221#define ADD_OBJECT(m, name, obj) \
5222 if (add_object(m, name, obj)) \
5223 return -1;
5224
5225#define ADD_CHECKED_OBJECT(m, name, obj) \
5226 { \
5227 PyObject *value = obj; \
5228 if (!value) \
5229 return -1; \
5230 ADD_OBJECT(m, name, value); \
5231 }
5232
5233 static int
5234populate_module(PyObject *m, object_adder add_object)
5235{
5236 int i;
5237
5238 for (i = 0; i < (int)(sizeof(numeric_constants)
5239 / sizeof(struct numeric_constant));
5240 ++i)
5241 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5242 PyInt_FromLong(numeric_constants[i].value));
5243
5244 for (i = 0; i < (int)(sizeof(object_constants)
5245 / sizeof(struct object_constant));
5246 ++i)
5247 {
5248 PyObject *value;
5249
5250 value = object_constants[i].value;
5251 Py_INCREF(value);
5252 ADD_OBJECT(m, object_constants[i].name, value);
5253 }
5254
5255 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5256 return -1;
5257 ADD_OBJECT(m, "error", VimError);
5258
Bram Moolenaara9922d62013-05-30 13:01:18 +02005259 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5260 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005261 ADD_CHECKED_OBJECT(m, "options",
5262 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
5263 return 0;
5264}