blob: c25be53e9b07250e8146b50bd103de6dc282adcd [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
25
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020026#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
27
28#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
29#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020030#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020031
32static int ConvertFromPyObject(PyObject *, typval_T *);
33static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020034static PyObject *WindowNew(win_T *, tabpage_T *);
35static PyObject *BufferNew (buf_T *);
36static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037
38static PyInt RangeStart;
39static PyInt RangeEnd;
40
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020041/*
42 * obtain a lock on the Vim data structures
43 */
44 static void
45Python_Lock_Vim(void)
46{
47}
48
49/*
50 * release a lock on the Vim data structures
51 */
52 static void
53Python_Release_Vim(void)
54{
55}
56
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020057/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020058 */
59
Bram Moolenaar2eea1982010-09-21 16:49:37 +020060/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020061typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020062
63static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020064
65typedef struct
66{
67 PyObject_HEAD
68 long softspace;
69 long error;
70} OutputObject;
71
Bram Moolenaar77045652012-09-21 13:46:06 +020072 static int
73OutputSetattr(PyObject *self, char *name, PyObject *val)
74{
75 if (val == NULL)
76 {
Bram Moolenaar8661b172013-05-15 15:44:28 +020077 PyErr_SetString(PyExc_AttributeError,
78 _("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +020079 return -1;
80 }
81
82 if (strcmp(name, "softspace") == 0)
83 {
84 if (!PyInt_Check(val))
85 {
86 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
87 return -1;
88 }
89
90 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
91 return 0;
92 }
93
94 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
95 return -1;
96}
97
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020098/* Buffer IO, we write one whole line at a time. */
99static garray_T io_ga = {0, 0, 1, 80, NULL};
100static writefn old_fn = NULL;
101
102 static void
103PythonIO_Flush(void)
104{
105 if (old_fn != NULL && io_ga.ga_len > 0)
106 {
107 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
108 old_fn((char_u *)io_ga.ga_data);
109 }
110 io_ga.ga_len = 0;
111}
112
113 static void
114writer(writefn fn, char_u *str, PyInt n)
115{
116 char_u *ptr;
117
118 /* Flush when switching output function. */
119 if (fn != old_fn)
120 PythonIO_Flush();
121 old_fn = fn;
122
123 /* Write each NL separated line. Text after the last NL is kept for
124 * writing later. */
125 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
126 {
127 PyInt len = ptr - str;
128
129 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
130 break;
131
132 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
133 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
134 fn((char_u *)io_ga.ga_data);
135 str = ptr + 1;
136 n -= len + 1;
137 io_ga.ga_len = 0;
138 }
139
140 /* Put the remaining text into io_ga for later printing. */
141 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
142 {
143 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
144 io_ga.ga_len += (int)n;
145 }
146}
147
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200148 static PyObject *
149OutputWrite(PyObject *self, PyObject *args)
150{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200151 Py_ssize_t len = 0;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200152 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200153 int error = ((OutputObject *)(self))->error;
154
Bram Moolenaar27564802011-09-07 19:30:21 +0200155 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200156 return NULL;
157
158 Py_BEGIN_ALLOW_THREADS
159 Python_Lock_Vim();
160 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
161 Python_Release_Vim();
162 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200163 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200164
165 Py_INCREF(Py_None);
166 return Py_None;
167}
168
169 static PyObject *
170OutputWritelines(PyObject *self, PyObject *args)
171{
172 PyInt n;
173 PyInt i;
174 PyObject *list;
175 int error = ((OutputObject *)(self))->error;
176
177 if (!PyArg_ParseTuple(args, "O", &list))
178 return NULL;
179 Py_INCREF(list);
180
Bram Moolenaardb913952012-06-29 12:54:53 +0200181 if (!PyList_Check(list))
182 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200183 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
184 Py_DECREF(list);
185 return NULL;
186 }
187
188 n = PyList_Size(list);
189
190 for (i = 0; i < n; ++i)
191 {
192 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200193 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200194 PyInt len;
195
Bram Moolenaardb913952012-06-29 12:54:53 +0200196 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
197 {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200198 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
199 Py_DECREF(list);
200 return NULL;
201 }
202
203 Py_BEGIN_ALLOW_THREADS
204 Python_Lock_Vim();
205 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
206 Python_Release_Vim();
207 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200208 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200209 }
210
211 Py_DECREF(list);
212 Py_INCREF(Py_None);
213 return Py_None;
214}
215
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100216 static PyObject *
217OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
218{
219 /* do nothing */
220 Py_INCREF(Py_None);
221 return Py_None;
222}
223
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200224/***************/
225
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200226static struct PyMethodDef OutputMethods[] = {
227 /* name, function, calling, documentation */
228 {"write", OutputWrite, 1, ""},
229 {"writelines", OutputWritelines, 1, ""},
230 {"flush", OutputFlush, 1, ""},
231 { NULL, NULL, 0, NULL}
232};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200233
234static OutputObject Output =
235{
236 PyObject_HEAD_INIT(&OutputType)
237 0,
238 0
239};
240
241static OutputObject Error =
242{
243 PyObject_HEAD_INIT(&OutputType)
244 0,
245 1
246};
247
248 static int
249PythonIO_Init_io(void)
250{
251 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
252 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
253
254 if (PyErr_Occurred())
255 {
256 EMSG(_("E264: Python: Error initialising I/O objects"));
257 return -1;
258 }
259
260 return 0;
261}
262
263
264static PyObject *VimError;
265
266/* Check to see whether a Vim error has been reported, or a keyboard
267 * interrupt has been detected.
268 */
269 static int
270VimErrorCheck(void)
271{
272 if (got_int)
273 {
274 PyErr_SetNone(PyExc_KeyboardInterrupt);
275 return 1;
276 }
277 else if (did_emsg && !PyErr_Occurred())
278 {
279 PyErr_SetNone(VimError);
280 return 1;
281 }
282
283 return 0;
284}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200285
286/* Vim module - Implementation
287 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200288
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200289 static PyObject *
290VimCommand(PyObject *self UNUSED, PyObject *args)
291{
292 char *cmd;
293 PyObject *result;
294
295 if (!PyArg_ParseTuple(args, "s", &cmd))
296 return NULL;
297
298 PyErr_Clear();
299
300 Py_BEGIN_ALLOW_THREADS
301 Python_Lock_Vim();
302
303 do_cmdline_cmd((char_u *)cmd);
304 update_screen(VALID);
305
306 Python_Release_Vim();
307 Py_END_ALLOW_THREADS
308
309 if (VimErrorCheck())
310 result = NULL;
311 else
312 result = Py_None;
313
314 Py_XINCREF(result);
315 return result;
316}
317
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200318/*
319 * Function to translate a typval_T into a PyObject; this will recursively
320 * translate lists/dictionaries into their Python equivalents.
321 *
322 * The depth parameter is to avoid infinite recursion, set it to 1 when
323 * you call VimToPython.
324 */
325 static PyObject *
326VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
327{
328 PyObject *result;
329 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200330 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200331
332 /* Avoid infinite recursion */
333 if (depth > 100)
334 {
335 Py_INCREF(Py_None);
336 result = Py_None;
337 return result;
338 }
339
340 /* Check if we run into a recursive loop. The item must be in lookupDict
341 * then and we can use it again. */
342 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
343 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
344 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200345 sprintf(ptrBuf, "%p",
346 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
347 : (void *)our_tv->vval.v_dict);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200348 result = PyDict_GetItemString(lookupDict, ptrBuf);
349 if (result != NULL)
350 {
351 Py_INCREF(result);
352 return result;
353 }
354 }
355
356 if (our_tv->v_type == VAR_STRING)
357 {
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200358 result = Py_BuildValue("s", our_tv->vval.v_string == NULL
359 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200360 }
361 else if (our_tv->v_type == VAR_NUMBER)
362 {
363 char buf[NUMBUFLEN];
364
365 /* For backwards compatibility numbers are stored as strings. */
366 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
367 result = Py_BuildValue("s", buf);
368 }
369# ifdef FEAT_FLOAT
370 else if (our_tv->v_type == VAR_FLOAT)
371 {
372 char buf[NUMBUFLEN];
373
374 sprintf(buf, "%f", our_tv->vval.v_float);
375 result = Py_BuildValue("s", buf);
376 }
377# endif
378 else if (our_tv->v_type == VAR_LIST)
379 {
380 list_T *list = our_tv->vval.v_list;
381 listitem_T *curr;
382
383 result = PyList_New(0);
384
385 if (list != NULL)
386 {
387 PyDict_SetItemString(lookupDict, ptrBuf, result);
388
389 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
390 {
391 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
392 PyList_Append(result, newObj);
393 Py_DECREF(newObj);
394 }
395 }
396 }
397 else if (our_tv->v_type == VAR_DICT)
398 {
399 result = PyDict_New();
400
401 if (our_tv->vval.v_dict != NULL)
402 {
403 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
404 long_u todo = ht->ht_used;
405 hashitem_T *hi;
406 dictitem_T *di;
407
408 PyDict_SetItemString(lookupDict, ptrBuf, result);
409
410 for (hi = ht->ht_array; todo > 0; ++hi)
411 {
412 if (!HASHITEM_EMPTY(hi))
413 {
414 --todo;
415
416 di = dict_lookup(hi);
417 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
418 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
419 Py_DECREF(newObj);
420 }
421 }
422 }
423 }
424 else
425 {
426 Py_INCREF(Py_None);
427 result = Py_None;
428 }
429
430 return result;
431}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200432
433 static PyObject *
Bram Moolenaar09092152010-08-08 16:38:42 +0200434VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200435{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200436 char *expr;
437 typval_T *our_tv;
438 PyObject *result;
439 PyObject *lookup_dict;
440
441 if (!PyArg_ParseTuple(args, "s", &expr))
442 return NULL;
443
444 Py_BEGIN_ALLOW_THREADS
445 Python_Lock_Vim();
446 our_tv = eval_expr((char_u *)expr, NULL);
447
448 Python_Release_Vim();
449 Py_END_ALLOW_THREADS
450
451 if (our_tv == NULL)
452 {
453 PyErr_SetVim(_("invalid expression"));
454 return NULL;
455 }
456
457 /* Convert the Vim type into a Python type. Create a dictionary that's
458 * used to check for recursive loops. */
459 lookup_dict = PyDict_New();
460 result = VimToPython(our_tv, 1, lookup_dict);
461 Py_DECREF(lookup_dict);
462
463
464 Py_BEGIN_ALLOW_THREADS
465 Python_Lock_Vim();
466 free_tv(our_tv);
467 Python_Release_Vim();
468 Py_END_ALLOW_THREADS
469
470 return result;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200471}
472
Bram Moolenaardb913952012-06-29 12:54:53 +0200473static PyObject *ConvertToPyObject(typval_T *);
474
475 static PyObject *
476VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED)
477{
Bram Moolenaardb913952012-06-29 12:54:53 +0200478 char *expr;
479 typval_T *our_tv;
480 PyObject *result;
481
482 if (!PyArg_ParseTuple(args, "s", &expr))
483 return NULL;
484
485 Py_BEGIN_ALLOW_THREADS
486 Python_Lock_Vim();
487 our_tv = eval_expr((char_u *)expr, NULL);
488
489 Python_Release_Vim();
490 Py_END_ALLOW_THREADS
491
492 if (our_tv == NULL)
493 {
494 PyErr_SetVim(_("invalid expression"));
495 return NULL;
496 }
497
498 result = ConvertToPyObject(our_tv);
499 Py_BEGIN_ALLOW_THREADS
500 Python_Lock_Vim();
501 free_tv(our_tv);
502 Python_Release_Vim();
503 Py_END_ALLOW_THREADS
504
505 return result;
Bram Moolenaardb913952012-06-29 12:54:53 +0200506}
507
508 static PyObject *
509VimStrwidth(PyObject *self UNUSED, PyObject *args)
510{
511 char *expr;
512
513 if (!PyArg_ParseTuple(args, "s", &expr))
514 return NULL;
515
Bram Moolenaara54bf402012-12-05 16:30:07 +0100516 return PyLong_FromLong(
517#ifdef FEAT_MBYTE
518 mb_string2cells((char_u *)expr, (int)STRLEN(expr))
519#else
520 STRLEN(expr)
521#endif
522 );
Bram Moolenaardb913952012-06-29 12:54:53 +0200523}
524
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200525/*
526 * Vim module - Definitions
527 */
528
529static struct PyMethodDef VimMethods[] = {
530 /* name, function, calling, documentation */
531 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
532 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200533 {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"},
534 {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200535 { NULL, NULL, 0, NULL }
536};
537
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200538/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200539 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200540 */
541
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200542static PyTypeObject IterType;
543
544typedef PyObject *(*nextfun)(void **);
545typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200546typedef int (*traversefun)(void *, visitproc, void *);
547typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200548
549/* Main purpose of this object is removing the need for do python initialization
550 * (i.e. PyType_Ready and setting type attributes) for a big bunch of objects.
551 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200552
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200553typedef struct
554{
555 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200556 void *cur;
557 nextfun next;
558 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200559 traversefun traverse;
560 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200561} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200562
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200563 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200564IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
565 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200566{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200567 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200568
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200569 self = PyObject_NEW(IterObject, &IterType);
570 self->cur = start;
571 self->next = next;
572 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200573 self->traverse = traverse;
574 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200575
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200576 return (PyObject *)(self);
577}
578
579 static void
580IterDestructor(PyObject *self)
581{
582 IterObject *this = (IterObject *)(self);
583
584 this->destruct(this->cur);
585
586 DESTRUCTOR_FINISH(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200587}
588
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200589 static int
590IterTraverse(PyObject *self, visitproc visit, void *arg)
591{
592 IterObject *this = (IterObject *)(self);
593
594 if (this->traverse != NULL)
595 return this->traverse(this->cur, visit, arg);
596 else
597 return 0;
598}
599
600 static int
601IterClear(PyObject *self)
602{
603 IterObject *this = (IterObject *)(self);
604
605 if (this->clear != NULL)
606 return this->clear(&this->cur);
607 else
608 return 0;
609}
610
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200611 static PyObject *
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200612IterNext(PyObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200614 IterObject *this = (IterObject *)(self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200615
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200616 return this->next(&this->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617}
618
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200619 static PyObject *
620IterIter(PyObject *self)
621{
622 return self;
623}
Bram Moolenaardfa38d42013-05-15 13:38:47 +0200624
Bram Moolenaardb913952012-06-29 12:54:53 +0200625typedef struct pylinkedlist_S {
626 struct pylinkedlist_S *pll_next;
627 struct pylinkedlist_S *pll_prev;
628 PyObject *pll_obj;
629} pylinkedlist_T;
630
631static pylinkedlist_T *lastdict = NULL;
632static pylinkedlist_T *lastlist = NULL;
633
634 static void
635pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
636{
637 if (ref->pll_prev == NULL)
638 {
639 if (ref->pll_next == NULL)
640 {
641 *last = NULL;
642 return;
643 }
644 }
645 else
646 ref->pll_prev->pll_next = ref->pll_next;
647
648 if (ref->pll_next == NULL)
649 *last = ref->pll_prev;
650 else
651 ref->pll_next->pll_prev = ref->pll_prev;
652}
653
654 static void
655pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
656{
657 if (*last == NULL)
658 ref->pll_prev = NULL;
659 else
660 {
661 (*last)->pll_next = ref;
662 ref->pll_prev = *last;
663 }
664 ref->pll_next = NULL;
665 ref->pll_obj = self;
666 *last = ref;
667}
668
669static PyTypeObject DictionaryType;
670
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200671#define DICTKEY_GET_NOTEMPTY(err) \
672 DICTKEY_GET(err) \
673 if (*key == NUL) \
674 { \
675 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
676 return err; \
677 }
678
Bram Moolenaardb913952012-06-29 12:54:53 +0200679typedef struct
680{
681 PyObject_HEAD
682 dict_T *dict;
683 pylinkedlist_T ref;
684} DictionaryObject;
685
686 static PyObject *
687DictionaryNew(dict_T *dict)
688{
689 DictionaryObject *self;
690
691 self = PyObject_NEW(DictionaryObject, &DictionaryType);
692 if (self == NULL)
693 return NULL;
694 self->dict = dict;
695 ++dict->dv_refcount;
696
697 pyll_add((PyObject *)(self), &self->ref, &lastdict);
698
699 return (PyObject *)(self);
700}
701
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200702 static void
703DictionaryDestructor(PyObject *self)
704{
705 DictionaryObject *this = ((DictionaryObject *) (self));
706
707 pyll_remove(&this->ref, &lastdict);
708 dict_unref(this->dict);
709
710 DESTRUCTOR_FINISH(self);
711}
712
Bram Moolenaardb913952012-06-29 12:54:53 +0200713 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200714DictionarySetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200715{
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200716 DictionaryObject *this = (DictionaryObject *)(self);
717
Bram Moolenaar66b79852012-09-21 14:00:35 +0200718 if (val == NULL)
719 {
720 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes"));
721 return -1;
722 }
723
724 if (strcmp(name, "locked") == 0)
725 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200726 if (this->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200727 {
728 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary"));
729 return -1;
730 }
731 else
732 {
Bram Moolenaarb983f752013-05-15 16:11:50 +0200733 int istrue = PyObject_IsTrue(val);
734 if (istrue == -1)
735 return -1;
736 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200737 this->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200738 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200739 this->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200740 }
741 return 0;
742 }
743 else
744 {
745 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute"));
746 return -1;
747 }
748}
749
750 static PyInt
Bram Moolenaardb913952012-06-29 12:54:53 +0200751DictionaryLength(PyObject *self)
752{
753 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
754}
755
756 static PyObject *
757DictionaryItem(PyObject *self, PyObject *keyObject)
758{
759 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200760 dictitem_T *di;
Bram Moolenaardb913952012-06-29 12:54:53 +0200761 DICTKEY_DECL
762
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200763 DICTKEY_GET_NOTEMPTY(NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200764
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200765 di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
766
Bram Moolenaar696c2112012-09-21 13:43:14 +0200767 DICTKEY_UNREF
768
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200769 if (di == NULL)
770 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200771 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200772 return NULL;
773 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200774
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200775 return ConvertToPyObject(&di->di_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200776}
777
778 static PyInt
779DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
780{
781 char_u *key;
782 typval_T tv;
783 dict_T *d = ((DictionaryObject *)(self))->dict;
784 dictitem_T *di;
785 DICTKEY_DECL
786
787 if (d->dv_lock)
788 {
789 PyErr_SetVim(_("dict is locked"));
790 return -1;
791 }
792
Bram Moolenaar231e1a12012-09-05 18:45:28 +0200793 DICTKEY_GET_NOTEMPTY(-1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200794
795 di = dict_find(d, key, -1);
796
797 if (valObject == NULL)
798 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200799 hashitem_T *hi;
800
Bram Moolenaardb913952012-06-29 12:54:53 +0200801 if (di == NULL)
802 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200803 DICTKEY_UNREF
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200804 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +0200805 return -1;
806 }
Bram Moolenaarf27839c2012-06-29 16:19:50 +0200807 hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaardb913952012-06-29 12:54:53 +0200808 hash_remove(&d->dv_hashtab, hi);
809 dictitem_free(di);
810 return 0;
811 }
812
813 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +0200814 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +0200815
816 if (di == NULL)
817 {
818 di = dictitem_alloc(key);
819 if (di == NULL)
820 {
821 PyErr_NoMemory();
822 return -1;
823 }
824 di->di_tv.v_lock = 0;
825
826 if (dict_add(d, di) == FAIL)
827 {
Bram Moolenaar696c2112012-09-21 13:43:14 +0200828 DICTKEY_UNREF
Bram Moolenaardb913952012-06-29 12:54:53 +0200829 vim_free(di);
830 PyErr_SetVim(_("failed to add key to dictionary"));
831 return -1;
832 }
833 }
834 else
835 clear_tv(&di->di_tv);
836
837 DICTKEY_UNREF
838
839 copy_tv(&tv, &di->di_tv);
840 return 0;
841}
842
843 static PyObject *
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100844DictionaryListKeys(PyObject *self UNUSED)
Bram Moolenaardb913952012-06-29 12:54:53 +0200845{
846 dict_T *dict = ((DictionaryObject *)(self))->dict;
847 long_u todo = dict->dv_hashtab.ht_used;
848 Py_ssize_t i = 0;
849 PyObject *r;
850 hashitem_T *hi;
851
852 r = PyList_New(todo);
853 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
854 {
855 if (!HASHITEM_EMPTY(hi))
856 {
857 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
858 --todo;
859 ++i;
860 }
861 }
862 return r;
863}
864
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200865static PyMappingMethods DictionaryAsMapping = {
866 (lenfunc) DictionaryLength,
867 (binaryfunc) DictionaryItem,
868 (objobjargproc) DictionaryAssItem,
869};
870
Bram Moolenaardb913952012-06-29 12:54:53 +0200871static struct PyMethodDef DictionaryMethods[] = {
872 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
873 { NULL, NULL, 0, NULL }
874};
875
876static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200877static PySequenceMethods ListAsSeq;
878static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +0200879
880typedef struct
881{
882 PyObject_HEAD
883 list_T *list;
884 pylinkedlist_T ref;
885} ListObject;
886
887 static PyObject *
888ListNew(list_T *list)
889{
890 ListObject *self;
891
892 self = PyObject_NEW(ListObject, &ListType);
893 if (self == NULL)
894 return NULL;
895 self->list = list;
896 ++list->lv_refcount;
897
898 pyll_add((PyObject *)(self), &self->ref, &lastlist);
899
900 return (PyObject *)(self);
901}
902
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200903 static void
904ListDestructor(PyObject *self)
905{
906 ListObject *this = (ListObject *)(self);
907
908 pyll_remove(&this->ref, &lastlist);
909 list_unref(this->list);
910
911 DESTRUCTOR_FINISH(self);
912}
913
Bram Moolenaardb913952012-06-29 12:54:53 +0200914 static int
915list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
916{
917 Py_ssize_t i;
918 Py_ssize_t lsize = PySequence_Size(obj);
919 PyObject *litem;
920 listitem_T *li;
921
922 for(i=0; i<lsize; i++)
923 {
924 li = listitem_alloc();
925 if (li == NULL)
926 {
927 PyErr_NoMemory();
928 return -1;
929 }
930 li->li_tv.v_lock = 0;
931
932 litem = PySequence_GetItem(obj, i);
933 if (litem == NULL)
934 return -1;
935 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
936 return -1;
937
938 list_append(l, li);
939 }
940 return 0;
941}
942
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 static PyInt
944ListLength(PyObject *self)
945{
946 return ((PyInt) (((ListObject *) (self))->list->lv_len));
947}
948
949 static PyObject *
950ListItem(PyObject *self, Py_ssize_t index)
951{
952 listitem_T *li;
953
954 if (index>=ListLength(self))
955 {
Bram Moolenaar8661b172013-05-15 15:44:28 +0200956 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200957 return NULL;
958 }
959 li = list_find(((ListObject *) (self))->list, (long) index);
960 if (li == NULL)
961 {
962 PyErr_SetVim(_("internal error: failed to get vim list item"));
963 return NULL;
964 }
965 return ConvertToPyObject(&li->li_tv);
966}
967
968#define PROC_RANGE \
969 if (last < 0) {\
970 if (last < -size) \
971 last = 0; \
972 else \
973 last += size; \
974 } \
975 if (first < 0) \
976 first = 0; \
977 if (first > size) \
978 first = size; \
979 if (last > size) \
980 last = size;
981
982 static PyObject *
983ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
984{
985 PyInt i;
986 PyInt size = ListLength(self);
987 PyInt n;
988 PyObject *list;
989 int reversed = 0;
990
991 PROC_RANGE
992 if (first >= last)
993 first = last;
994
995 n = last-first;
996 list = PyList_New(n);
997 if (list == NULL)
998 return NULL;
999
1000 for (i = 0; i < n; ++i)
1001 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001002 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001003 if (item == NULL)
1004 {
1005 Py_DECREF(list);
1006 return NULL;
1007 }
1008
1009 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
1010 {
1011 Py_DECREF(item);
1012 Py_DECREF(list);
1013 return NULL;
1014 }
1015 }
1016
1017 return list;
1018}
1019
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001020typedef struct
1021{
1022 listwatch_T lw;
1023 list_T *list;
1024} listiterinfo_T;
1025
1026 static void
1027ListIterDestruct(listiterinfo_T *lii)
1028{
1029 list_rem_watch(lii->list, &lii->lw);
1030 PyMem_Free(lii);
1031}
1032
1033 static PyObject *
1034ListIterNext(listiterinfo_T **lii)
1035{
1036 PyObject *r;
1037
1038 if (!((*lii)->lw.lw_item))
1039 return NULL;
1040
1041 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1042 return NULL;
1043
1044 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1045
1046 return r;
1047}
1048
1049 static PyObject *
1050ListIter(PyObject *self)
1051{
1052 listiterinfo_T *lii;
1053 list_T *l = ((ListObject *) (self))->list;
1054
1055 if (!(lii = PyMem_New(listiterinfo_T, 1)))
1056 {
1057 PyErr_NoMemory();
1058 return NULL;
1059 }
1060
1061 list_add_watch(l, &lii->lw);
1062 lii->lw.lw_item = l->lv_first;
1063 lii->list = l;
1064
1065 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001066 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
1067 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001068}
1069
Bram Moolenaardb913952012-06-29 12:54:53 +02001070 static int
1071ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
1072{
1073 typval_T tv;
1074 list_T *l = ((ListObject *) (self))->list;
1075 listitem_T *li;
1076 Py_ssize_t length = ListLength(self);
1077
1078 if (l->lv_lock)
1079 {
1080 PyErr_SetVim(_("list is locked"));
1081 return -1;
1082 }
1083 if (index>length || (index==length && obj==NULL))
1084 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001085 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001086 return -1;
1087 }
1088
1089 if (obj == NULL)
1090 {
1091 li = list_find(l, (long) index);
1092 list_remove(l, li, li);
1093 clear_tv(&li->li_tv);
1094 vim_free(li);
1095 return 0;
1096 }
1097
1098 if (ConvertFromPyObject(obj, &tv) == -1)
1099 return -1;
1100
1101 if (index == length)
1102 {
1103 if (list_append_tv(l, &tv) == FAIL)
1104 {
1105 PyErr_SetVim(_("Failed to add item to list"));
1106 return -1;
1107 }
1108 }
1109 else
1110 {
1111 li = list_find(l, (long) index);
1112 clear_tv(&li->li_tv);
1113 copy_tv(&tv, &li->li_tv);
1114 }
1115 return 0;
1116}
1117
1118 static int
1119ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
1120{
1121 PyInt size = ListLength(self);
1122 Py_ssize_t i;
1123 Py_ssize_t lsize;
1124 PyObject *litem;
1125 listitem_T *li;
1126 listitem_T *next;
1127 typval_T v;
1128 list_T *l = ((ListObject *) (self))->list;
1129
1130 if (l->lv_lock)
1131 {
1132 PyErr_SetVim(_("list is locked"));
1133 return -1;
1134 }
1135
1136 PROC_RANGE
1137
1138 if (first == size)
1139 li = NULL;
1140 else
1141 {
1142 li = list_find(l, (long) first);
1143 if (li == NULL)
1144 {
1145 PyErr_SetVim(_("internal error: no vim list item"));
1146 return -1;
1147 }
1148 if (last > first)
1149 {
1150 i = last - first;
1151 while (i-- && li != NULL)
1152 {
1153 next = li->li_next;
1154 listitem_remove(l, li);
1155 li = next;
1156 }
1157 }
1158 }
1159
1160 if (obj == NULL)
1161 return 0;
1162
1163 if (!PyList_Check(obj))
1164 {
1165 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
1166 return -1;
1167 }
1168
1169 lsize = PyList_Size(obj);
1170
1171 for(i=0; i<lsize; i++)
1172 {
1173 litem = PyList_GetItem(obj, i);
1174 if (litem == NULL)
1175 return -1;
1176 if (ConvertFromPyObject(litem, &v) == -1)
1177 return -1;
1178 if (list_insert_tv(l, &v, li) == FAIL)
1179 {
1180 PyErr_SetVim(_("internal error: failed to add item to list"));
1181 return -1;
1182 }
1183 }
1184 return 0;
1185}
1186
1187 static PyObject *
1188ListConcatInPlace(PyObject *self, PyObject *obj)
1189{
1190 list_T *l = ((ListObject *) (self))->list;
1191 PyObject *lookup_dict;
1192
1193 if (l->lv_lock)
1194 {
1195 PyErr_SetVim(_("list is locked"));
1196 return NULL;
1197 }
1198
1199 if (!PySequence_Check(obj))
1200 {
1201 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
1202 return NULL;
1203 }
1204
1205 lookup_dict = PyDict_New();
1206 if (list_py_concat(l, obj, lookup_dict) == -1)
1207 {
1208 Py_DECREF(lookup_dict);
1209 return NULL;
1210 }
1211 Py_DECREF(lookup_dict);
1212
1213 Py_INCREF(self);
1214 return self;
1215}
1216
Bram Moolenaar66b79852012-09-21 14:00:35 +02001217 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001218ListSetattr(PyObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001219{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001220 ListObject *this = (ListObject *)(self);
1221
Bram Moolenaar66b79852012-09-21 14:00:35 +02001222 if (val == NULL)
1223 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001224 PyErr_SetString(PyExc_AttributeError,
1225 _("cannot delete vim.dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001226 return -1;
1227 }
1228
1229 if (strcmp(name, "locked") == 0)
1230 {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001231 if (this->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001232 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001233 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001234 return -1;
1235 }
1236 else
1237 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001238 int istrue = PyObject_IsTrue(val);
1239 if (istrue == -1)
1240 return -1;
1241 else if (istrue)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001242 this->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001243 else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001244 this->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001245 }
1246 return 0;
1247 }
1248 else
1249 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001250 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001251 return -1;
1252 }
1253}
1254
Bram Moolenaardb913952012-06-29 12:54:53 +02001255static struct PyMethodDef ListMethods[] = {
1256 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
1257 { NULL, NULL, 0, NULL }
1258};
1259
1260typedef struct
1261{
1262 PyObject_HEAD
1263 char_u *name;
1264} FunctionObject;
1265
1266static PyTypeObject FunctionType;
1267
1268 static PyObject *
1269FunctionNew(char_u *name)
1270{
1271 FunctionObject *self;
1272
1273 self = PyObject_NEW(FunctionObject, &FunctionType);
1274 if (self == NULL)
1275 return NULL;
1276 self->name = PyMem_New(char_u, STRLEN(name) + 1);
1277 if (self->name == NULL)
1278 {
1279 PyErr_NoMemory();
1280 return NULL;
1281 }
1282 STRCPY(self->name, name);
1283 func_ref(name);
1284 return (PyObject *)(self);
1285}
1286
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001287 static void
1288FunctionDestructor(PyObject *self)
1289{
1290 FunctionObject *this = (FunctionObject *) (self);
1291
1292 func_unref(this->name);
1293 PyMem_Del(this->name);
1294
1295 DESTRUCTOR_FINISH(self);
1296}
1297
Bram Moolenaardb913952012-06-29 12:54:53 +02001298 static PyObject *
1299FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
1300{
1301 FunctionObject *this = (FunctionObject *)(self);
1302 char_u *name = this->name;
1303 typval_T args;
1304 typval_T selfdicttv;
1305 typval_T rettv;
1306 dict_T *selfdict = NULL;
1307 PyObject *selfdictObject;
1308 PyObject *result;
1309 int error;
1310
1311 if (ConvertFromPyObject(argsObject, &args) == -1)
1312 return NULL;
1313
1314 if (kwargs != NULL)
1315 {
1316 selfdictObject = PyDict_GetItemString(kwargs, "self");
1317 if (selfdictObject != NULL)
1318 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001319 if (!PyMapping_Check(selfdictObject))
Bram Moolenaardb913952012-06-29 12:54:53 +02001320 {
Bram Moolenaar9581b5f2012-07-25 15:36:04 +02001321 PyErr_SetString(PyExc_TypeError,
1322 _("'self' argument must be a dictionary"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001323 clear_tv(&args);
1324 return NULL;
1325 }
1326 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
1327 return NULL;
1328 selfdict = selfdicttv.vval.v_dict;
1329 }
1330 }
1331
Bram Moolenaar71700b82013-05-15 17:49:05 +02001332 Py_BEGIN_ALLOW_THREADS
1333 Python_Lock_Vim();
1334
Bram Moolenaardb913952012-06-29 12:54:53 +02001335 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02001336
1337 Python_Release_Vim();
1338 Py_END_ALLOW_THREADS
1339
Bram Moolenaardb913952012-06-29 12:54:53 +02001340 if (error != OK)
1341 {
1342 result = NULL;
1343 PyErr_SetVim(_("failed to run function"));
1344 }
1345 else
1346 result = ConvertToPyObject(&rettv);
1347
1348 /* FIXME Check what should really be cleared. */
1349 clear_tv(&args);
1350 clear_tv(&rettv);
1351 /*
1352 * if (selfdict!=NULL)
1353 * clear_tv(selfdicttv);
1354 */
1355
1356 return result;
1357}
1358
1359static struct PyMethodDef FunctionMethods[] = {
1360 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
1361 { NULL, NULL, 0, NULL }
1362};
1363
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001364/*
1365 * Options object
1366 */
1367
1368static PyTypeObject OptionsType;
1369
1370typedef int (*checkfun)(void *);
1371
1372typedef struct
1373{
1374 PyObject_HEAD
1375 int opt_type;
1376 void *from;
1377 checkfun Check;
1378 PyObject *fromObj;
1379} OptionsObject;
1380
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001381 static int
1382dummy_check(void *arg UNUSED)
1383{
1384 return 0;
1385}
1386
1387 static PyObject *
1388OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
1389{
1390 OptionsObject *self;
1391
1392 self = PyObject_NEW(OptionsObject, &OptionsType);
1393 if (self == NULL)
1394 return NULL;
1395
1396 self->opt_type = opt_type;
1397 self->from = from;
1398 self->Check = Check;
1399 self->fromObj = fromObj;
1400 if (fromObj)
1401 Py_INCREF(fromObj);
1402
1403 return (PyObject *)(self);
1404}
1405
1406 static void
1407OptionsDestructor(PyObject *self)
1408{
1409 if (((OptionsObject *)(self))->fromObj)
1410 Py_DECREF(((OptionsObject *)(self))->fromObj);
1411 DESTRUCTOR_FINISH(self);
1412}
1413
1414 static int
1415OptionsTraverse(PyObject *self, visitproc visit, void *arg)
1416{
1417 Py_VISIT(((OptionsObject *)(self))->fromObj);
1418 return 0;
1419}
1420
1421 static int
1422OptionsClear(PyObject *self)
1423{
1424 Py_CLEAR(((OptionsObject *)(self))->fromObj);
1425 return 0;
1426}
1427
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001428 static PyObject *
1429OptionsItem(OptionsObject *this, PyObject *keyObject)
1430{
1431 char_u *key;
1432 int flags;
1433 long numval;
1434 char_u *stringval;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001435 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001436
1437 if (this->Check(this->from))
1438 return NULL;
1439
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001440 DICTKEY_GET_NOTEMPTY(NULL)
1441
1442 flags = get_option_value_strict(key, &numval, &stringval,
1443 this->opt_type, this->from);
1444
1445 DICTKEY_UNREF
1446
1447 if (flags == 0)
1448 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001449 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001450 return NULL;
1451 }
1452
1453 if (flags & SOPT_UNSET)
1454 {
1455 Py_INCREF(Py_None);
1456 return Py_None;
1457 }
1458 else if (flags & SOPT_BOOL)
1459 {
1460 PyObject *r;
1461 r = numval ? Py_True : Py_False;
1462 Py_INCREF(r);
1463 return r;
1464 }
1465 else if (flags & SOPT_NUM)
1466 return PyInt_FromLong(numval);
1467 else if (flags & SOPT_STRING)
1468 {
1469 if (stringval)
1470 return PyBytes_FromString((char *) stringval);
1471 else
1472 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001473 PyErr_SetString(PyExc_RuntimeError,
1474 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001475 return NULL;
1476 }
1477 }
1478 else
1479 {
1480 PyErr_SetVim("Internal error: unknown option type. Should not happen");
1481 return NULL;
1482 }
1483}
1484
1485 static int
1486set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
1487 char_u *key;
1488 int numval;
1489 char_u *stringval;
1490 int opt_flags;
1491 int opt_type;
1492 void *from;
1493{
1494 win_T *save_curwin;
1495 tabpage_T *save_curtab;
Bram Moolenaar105bc352013-05-17 16:03:57 +02001496 buf_T *save_curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001497 int r = 0;
1498
1499 switch (opt_type)
1500 {
1501 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001502 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
1503 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001504 {
1505 PyErr_SetVim("Problem while switching windows.");
1506 return -1;
1507 }
1508 set_option_value(key, numval, stringval, opt_flags);
1509 restore_win(save_curwin, save_curtab);
1510 break;
1511 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02001512 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001513 set_option_value(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02001514 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001515 break;
1516 case SREQ_GLOBAL:
1517 set_option_value(key, numval, stringval, opt_flags);
1518 break;
1519 }
1520 return r;
1521}
1522
1523 static int
1524OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject)
1525{
1526 char_u *key;
1527 int flags;
1528 int opt_flags;
1529 int r = 0;
Bram Moolenaar161fb5e2013-05-06 06:26:15 +02001530 DICTKEY_DECL
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001531
1532 if (this->Check(this->from))
1533 return -1;
1534
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001535 DICTKEY_GET_NOTEMPTY(-1)
1536
1537 flags = get_option_value_strict(key, NULL, NULL,
1538 this->opt_type, this->from);
1539
1540 DICTKEY_UNREF
1541
1542 if (flags == 0)
1543 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001544 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001545 return -1;
1546 }
1547
1548 if (valObject == NULL)
1549 {
1550 if (this->opt_type == SREQ_GLOBAL)
1551 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001552 PyErr_SetString(PyExc_ValueError,
1553 _("unable to unset global option"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001554 return -1;
1555 }
1556 else if (!(flags & SOPT_GLOBAL))
1557 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001558 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
1559 "without global value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001560 return -1;
1561 }
1562 else
1563 {
1564 unset_global_local_option(key, this->from);
1565 return 0;
1566 }
1567 }
1568
1569 opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL);
1570
1571 if (flags & SOPT_BOOL)
1572 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001573 int istrue = PyObject_IsTrue(valObject);
1574 if (istrue == -1)
1575 return -1;
1576 r = set_option_value_for(key, istrue, NULL,
Bram Moolenaar03db85b2013-05-15 14:51:35 +02001577 opt_flags, this->opt_type, this->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001578 }
1579 else if (flags & SOPT_NUM)
1580 {
1581 int val;
1582
1583#if PY_MAJOR_VERSION < 3
1584 if (PyInt_Check(valObject))
1585 val = PyInt_AsLong(valObject);
1586 else
1587#endif
1588 if (PyLong_Check(valObject))
1589 val = PyLong_AsLong(valObject);
1590 else
1591 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001592 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001593 return -1;
1594 }
1595
1596 r = set_option_value_for(key, val, NULL, opt_flags,
1597 this->opt_type, this->from);
1598 }
1599 else
1600 {
1601 char_u *val;
1602 if (PyBytes_Check(valObject))
1603 {
1604
1605 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1)
1606 return -1;
1607 if (val == NULL)
1608 return -1;
1609
1610 val = vim_strsave(val);
1611 }
1612 else if (PyUnicode_Check(valObject))
1613 {
1614 PyObject *bytes;
1615
1616 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL);
1617 if (bytes == NULL)
1618 return -1;
1619
1620 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1)
1621 return -1;
1622 if (val == NULL)
1623 return -1;
1624
1625 val = vim_strsave(val);
1626 Py_XDECREF(bytes);
1627 }
1628 else
1629 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001630 PyErr_SetString(PyExc_TypeError, _("object must be string"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001631 return -1;
1632 }
1633
1634 r = set_option_value_for(key, 0, val, opt_flags,
1635 this->opt_type, this->from);
1636 vim_free(val);
1637 }
1638
1639 return r;
1640}
1641
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001642static PyMappingMethods OptionsAsMapping = {
1643 (lenfunc) NULL,
1644 (binaryfunc) OptionsItem,
1645 (objobjargproc) OptionsAssItem,
1646};
1647
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001648/* Tabpage object
1649 */
1650
1651typedef struct
1652{
1653 PyObject_HEAD
1654 tabpage_T *tab;
1655} TabPageObject;
1656
1657static PyObject *WinListNew(TabPageObject *tabObject);
1658
1659static PyTypeObject TabPageType;
1660
1661 static int
1662CheckTabPage(TabPageObject *this)
1663{
1664 if (this->tab == INVALID_TABPAGE_VALUE)
1665 {
1666 PyErr_SetVim(_("attempt to refer to deleted tab page"));
1667 return -1;
1668 }
1669
1670 return 0;
1671}
1672
1673 static PyObject *
1674TabPageNew(tabpage_T *tab)
1675{
1676 TabPageObject *self;
1677
1678 if (TAB_PYTHON_REF(tab))
1679 {
1680 self = TAB_PYTHON_REF(tab);
1681 Py_INCREF(self);
1682 }
1683 else
1684 {
1685 self = PyObject_NEW(TabPageObject, &TabPageType);
1686 if (self == NULL)
1687 return NULL;
1688 self->tab = tab;
1689 TAB_PYTHON_REF(tab) = self;
1690 }
1691
1692 return (PyObject *)(self);
1693}
1694
1695 static void
1696TabPageDestructor(PyObject *self)
1697{
1698 TabPageObject *this = (TabPageObject *)(self);
1699
1700 if (this->tab && this->tab != INVALID_TABPAGE_VALUE)
1701 TAB_PYTHON_REF(this->tab) = NULL;
1702
1703 DESTRUCTOR_FINISH(self);
1704}
1705
1706 static PyObject *
1707TabPageAttr(TabPageObject *this, char *name)
1708{
1709 if (strcmp(name, "windows") == 0)
1710 return WinListNew(this);
1711 else if (strcmp(name, "number") == 0)
1712 return PyLong_FromLong((long) get_tab_number(this->tab));
1713 else if (strcmp(name, "vars") == 0)
1714 return DictionaryNew(this->tab->tp_vars);
1715 else if (strcmp(name, "window") == 0)
1716 {
1717 /* For current tab window.c does not bother to set or update tp_curwin
1718 */
1719 if (this->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001720 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001721 else
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001722 return WindowNew(this->tab->tp_curwin, this->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001723 }
1724 return NULL;
1725}
1726
1727 static PyObject *
1728TabPageRepr(PyObject *self)
1729{
1730 static char repr[100];
1731 TabPageObject *this = (TabPageObject *)(self);
1732
1733 if (this->tab == INVALID_TABPAGE_VALUE)
1734 {
1735 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self));
1736 return PyString_FromString(repr);
1737 }
1738 else
1739 {
1740 int t = get_tab_number(this->tab);
1741
1742 if (t == 0)
1743 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"),
1744 (self));
1745 else
1746 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1);
1747
1748 return PyString_FromString(repr);
1749 }
1750}
1751
1752static struct PyMethodDef TabPageMethods[] = {
1753 /* name, function, calling, documentation */
1754 { NULL, NULL, 0, NULL }
1755};
1756
1757/*
1758 * Window list object
1759 */
1760
1761static PyTypeObject TabListType;
1762static PySequenceMethods TabListAsSeq;
1763
1764typedef struct
1765{
1766 PyObject_HEAD
1767} TabListObject;
1768
1769 static PyInt
1770TabListLength(PyObject *self UNUSED)
1771{
1772 tabpage_T *tp = first_tabpage;
1773 PyInt n = 0;
1774
1775 while (tp != NULL)
1776 {
1777 ++n;
1778 tp = tp->tp_next;
1779 }
1780
1781 return n;
1782}
1783
1784 static PyObject *
1785TabListItem(PyObject *self UNUSED, PyInt n)
1786{
1787 tabpage_T *tp;
1788
1789 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
1790 if (n == 0)
1791 return TabPageNew(tp);
1792
1793 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
1794 return NULL;
1795}
1796
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001797/* Window object
1798 */
1799
1800typedef struct
1801{
1802 PyObject_HEAD
1803 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001804 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001805} WindowObject;
1806
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001807static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001808
1809 static int
1810CheckWindow(WindowObject *this)
1811{
1812 if (this->win == INVALID_WINDOW_VALUE)
1813 {
1814 PyErr_SetVim(_("attempt to refer to deleted window"));
1815 return -1;
1816 }
1817
1818 return 0;
1819}
1820
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001821 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001822WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02001823{
1824 /* We need to handle deletion of windows underneath us.
1825 * If we add a "w_python*_ref" field to the win_T structure,
1826 * then we can get at it in win_free() in vim. We then
1827 * need to create only ONE Python object per window - if
1828 * we try to create a second, just INCREF the existing one
1829 * and return it. The (single) Python object referring to
1830 * the window is stored in "w_python*_ref".
1831 * On a win_free() we set the Python object's win_T* field
1832 * to an invalid value. We trap all uses of a window
1833 * object, and reject them if the win_T* field is invalid.
1834 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001835 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02001836 * w_python_ref and w_python3_ref fields respectively.
1837 */
1838
1839 WindowObject *self;
1840
1841 if (WIN_PYTHON_REF(win))
1842 {
1843 self = WIN_PYTHON_REF(win);
1844 Py_INCREF(self);
1845 }
1846 else
1847 {
1848 self = PyObject_NEW(WindowObject, &WindowType);
1849 if (self == NULL)
1850 return NULL;
1851 self->win = win;
1852 WIN_PYTHON_REF(win) = self;
1853 }
1854
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001855 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1856
Bram Moolenaar971db462013-05-12 18:44:48 +02001857 return (PyObject *)(self);
1858}
1859
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001860 static void
1861WindowDestructor(PyObject *self)
1862{
1863 WindowObject *this = (WindowObject *)(self);
1864
1865 if (this->win && this->win != INVALID_WINDOW_VALUE)
1866 WIN_PYTHON_REF(this->win) = NULL;
1867
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001868 Py_DECREF(((PyObject *)(this->tabObject)));
1869
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001870 DESTRUCTOR_FINISH(self);
1871}
1872
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001873 static win_T *
1874get_firstwin(TabPageObject *tabObject)
1875{
1876 if (tabObject)
1877 {
1878 if (CheckTabPage(tabObject))
1879 return NULL;
1880 /* For current tab window.c does not bother to set or update tp_firstwin
1881 */
1882 else if (tabObject->tab == curtab)
1883 return firstwin;
1884 else
1885 return tabObject->tab->tp_firstwin;
1886 }
1887 else
1888 return firstwin;
1889}
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001890 static int
1891WindowTraverse(PyObject *self, visitproc visit, void *arg)
1892{
1893 Py_VISIT(((PyObject *)(((WindowObject *)(self))->tabObject)));
1894 return 0;
1895}
1896
1897 static int
1898WindowClear(PyObject *self)
1899{
1900 Py_CLEAR((((WindowObject *)(self))->tabObject));
1901 return 0;
1902}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001903
Bram Moolenaar971db462013-05-12 18:44:48 +02001904 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001905WindowAttr(WindowObject *this, char *name)
1906{
1907 if (strcmp(name, "buffer") == 0)
1908 return (PyObject *)BufferNew(this->win->w_buffer);
1909 else if (strcmp(name, "cursor") == 0)
1910 {
1911 pos_T *pos = &this->win->w_cursor;
1912
1913 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1914 }
1915 else if (strcmp(name, "height") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001916 return PyLong_FromLong((long)(this->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001917#ifdef FEAT_WINDOWS
1918 else if (strcmp(name, "row") == 0)
1919 return PyLong_FromLong((long)(this->win->w_winrow));
1920#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001921#ifdef FEAT_VERTSPLIT
1922 else if (strcmp(name, "width") == 0)
Bram Moolenaar99add412013-05-12 19:09:51 +02001923 return PyLong_FromLong((long)(W_WIDTH(this->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02001924 else if (strcmp(name, "col") == 0)
1925 return PyLong_FromLong((long)(W_WINCOL(this->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001926#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001927 else if (strcmp(name, "vars") == 0)
1928 return DictionaryNew(this->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001929 else if (strcmp(name, "options") == 0)
1930 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1931 (PyObject *) this);
Bram Moolenaar6d216452013-05-12 19:00:41 +02001932 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001933 {
1934 if (CheckTabPage(this->tabObject))
1935 return NULL;
1936 return PyLong_FromLong((long)
1937 get_win_number(this->win, get_firstwin(this->tabObject)));
1938 }
1939 else if (strcmp(name, "tabpage") == 0)
1940 {
1941 Py_INCREF(this->tabObject);
1942 return (PyObject *)(this->tabObject);
1943 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001944 else if (strcmp(name,"__members__") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02001945 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1946 "vars", "options", "number", "row", "col", "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001947 else
1948 return NULL;
1949}
1950
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001951 static int
1952WindowSetattr(PyObject *self, char *name, PyObject *val)
1953{
1954 WindowObject *this = (WindowObject *)(self);
1955
1956 if (CheckWindow(this))
1957 return -1;
1958
1959 if (strcmp(name, "buffer") == 0)
1960 {
1961 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1962 return -1;
1963 }
1964 else if (strcmp(name, "cursor") == 0)
1965 {
1966 long lnum;
1967 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001968
1969 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1970 return -1;
1971
1972 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
1973 {
1974 PyErr_SetVim(_("cursor position outside buffer"));
1975 return -1;
1976 }
1977
1978 /* Check for keyboard interrupts */
1979 if (VimErrorCheck())
1980 return -1;
1981
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001982 this->win->w_cursor.lnum = lnum;
1983 this->win->w_cursor.col = col;
1984#ifdef FEAT_VIRTUALEDIT
1985 this->win->w_cursor.coladd = 0;
1986#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001987 /* When column is out of range silently correct it. */
1988 check_cursor_col_win(this->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001989
Bram Moolenaar03a807a2011-07-07 15:08:58 +02001990 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001991 return 0;
1992 }
1993 else if (strcmp(name, "height") == 0)
1994 {
1995 int height;
1996 win_T *savewin;
1997
1998 if (!PyArg_Parse(val, "i", &height))
1999 return -1;
2000
2001#ifdef FEAT_GUI
2002 need_mouse_correct = TRUE;
2003#endif
2004 savewin = curwin;
2005 curwin = this->win;
2006 win_setheight(height);
2007 curwin = savewin;
2008
2009 /* Check for keyboard interrupts */
2010 if (VimErrorCheck())
2011 return -1;
2012
2013 return 0;
2014 }
2015#ifdef FEAT_VERTSPLIT
2016 else if (strcmp(name, "width") == 0)
2017 {
2018 int width;
2019 win_T *savewin;
2020
2021 if (!PyArg_Parse(val, "i", &width))
2022 return -1;
2023
2024#ifdef FEAT_GUI
2025 need_mouse_correct = TRUE;
2026#endif
2027 savewin = curwin;
2028 curwin = this->win;
2029 win_setwidth(width);
2030 curwin = savewin;
2031
2032 /* Check for keyboard interrupts */
2033 if (VimErrorCheck())
2034 return -1;
2035
2036 return 0;
2037 }
2038#endif
2039 else
2040 {
2041 PyErr_SetString(PyExc_AttributeError, name);
2042 return -1;
2043 }
2044}
2045
2046 static PyObject *
2047WindowRepr(PyObject *self)
2048{
2049 static char repr[100];
2050 WindowObject *this = (WindowObject *)(self);
2051
2052 if (this->win == INVALID_WINDOW_VALUE)
2053 {
2054 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
2055 return PyString_FromString(repr);
2056 }
2057 else
2058 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002059 int w = get_win_number(this->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002060
Bram Moolenaar6d216452013-05-12 19:00:41 +02002061 if (w == 0)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002062 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
2063 (self));
2064 else
Bram Moolenaar6d216452013-05-12 19:00:41 +02002065 vim_snprintf(repr, 100, _("<window %d>"), w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002066
2067 return PyString_FromString(repr);
2068 }
2069}
2070
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002071static struct PyMethodDef WindowMethods[] = {
2072 /* name, function, calling, documentation */
2073 { NULL, NULL, 0, NULL }
2074};
2075
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002076/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002077 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002078 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002079
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002080static PyTypeObject WinListType;
2081static PySequenceMethods WinListAsSeq;
2082
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002083typedef struct
2084{
2085 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002086 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002087} WinListObject;
2088
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002089 static PyObject *
2090WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002091{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002092 WinListObject *self;
2093
2094 self = PyObject_NEW(WinListObject, &WinListType);
2095 self->tabObject = tabObject;
2096 Py_INCREF(tabObject);
2097
2098 return (PyObject *)(self);
2099}
2100
2101 static void
2102WinListDestructor(PyObject *self)
2103{
2104 TabPageObject *tabObject = ((WinListObject *)(self))->tabObject;
2105
2106 if (tabObject)
2107 Py_DECREF((PyObject *)(tabObject));
2108
2109 DESTRUCTOR_FINISH(self);
2110}
2111
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002112 static PyInt
2113WinListLength(PyObject *self)
2114{
2115 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002116 PyInt n = 0;
2117
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002118 if (!(w = get_firstwin(((WinListObject *)(self))->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002119 return -1;
2120
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002121 while (w != NULL)
2122 {
2123 ++n;
2124 w = W_NEXT(w);
2125 }
2126
2127 return n;
2128}
2129
2130 static PyObject *
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002131WinListItem(PyObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002132{
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002133 WinListObject *this = ((WinListObject *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002134 win_T *w;
2135
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002136 if (!(w = get_firstwin(this->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002137 return NULL;
2138
2139 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002140 if (n == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002141 return WindowNew(w, this->tabObject? this->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002142
2143 PyErr_SetString(PyExc_IndexError, _("no such window"));
2144 return NULL;
2145}
2146
2147/* Convert a Python string into a Vim line.
2148 *
2149 * The result is in allocated memory. All internal nulls are replaced by
2150 * newline characters. It is an error for the string to contain newline
2151 * characters.
2152 *
2153 * On errors, the Python exception data is set, and NULL is returned.
2154 */
2155 static char *
2156StringToLine(PyObject *obj)
2157{
2158 const char *str;
2159 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002160 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002161 PyInt len;
2162 PyInt i;
2163 char *p;
2164
2165 if (obj == NULL || !PyString_Check(obj))
2166 {
2167 PyErr_BadArgument();
2168 return NULL;
2169 }
2170
Bram Moolenaar19e60942011-06-19 00:27:51 +02002171 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
2172 str = PyString_AsString(bytes);
2173 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002174
2175 /*
2176 * Error checking: String must not contain newlines, as we
2177 * are replacing a single line, and we must replace it with
2178 * a single line.
2179 * A trailing newline is removed, so that append(f.readlines()) works.
2180 */
2181 p = memchr(str, '\n', len);
2182 if (p != NULL)
2183 {
2184 if (p == str + len - 1)
2185 --len;
2186 else
2187 {
2188 PyErr_SetVim(_("string cannot contain newlines"));
2189 return NULL;
2190 }
2191 }
2192
2193 /* Create a copy of the string, with internal nulls replaced by
2194 * newline characters, as is the vim convention.
2195 */
2196 save = (char *)alloc((unsigned)(len+1));
2197 if (save == NULL)
2198 {
2199 PyErr_NoMemory();
2200 return NULL;
2201 }
2202
2203 for (i = 0; i < len; ++i)
2204 {
2205 if (str[i] == '\0')
2206 save[i] = '\n';
2207 else
2208 save[i] = str[i];
2209 }
2210
2211 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02002212 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002213
2214 return save;
2215}
2216
2217/* Get a line from the specified buffer. The line number is
2218 * in Vim format (1-based). The line is returned as a Python
2219 * string object.
2220 */
2221 static PyObject *
2222GetBufferLine(buf_T *buf, PyInt n)
2223{
2224 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2225}
2226
2227
2228/* Get a list of lines from the specified buffer. The line numbers
2229 * are in Vim format (1-based). The range is from lo up to, but not
2230 * including, hi. The list is returned as a Python list of string objects.
2231 */
2232 static PyObject *
2233GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
2234{
2235 PyInt i;
2236 PyInt n = hi - lo;
2237 PyObject *list = PyList_New(n);
2238
2239 if (list == NULL)
2240 return NULL;
2241
2242 for (i = 0; i < n; ++i)
2243 {
2244 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2245
2246 /* Error check - was the Python string creation OK? */
2247 if (str == NULL)
2248 {
2249 Py_DECREF(list);
2250 return NULL;
2251 }
2252
2253 /* Set the list item */
2254 if (PyList_SetItem(list, i, str))
2255 {
2256 Py_DECREF(str);
2257 Py_DECREF(list);
2258 return NULL;
2259 }
2260 }
2261
2262 /* The ownership of the Python list is passed to the caller (ie,
2263 * the caller should Py_DECREF() the object when it is finished
2264 * with it).
2265 */
2266
2267 return list;
2268}
2269
2270/*
2271 * Check if deleting lines made the cursor position invalid.
2272 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2273 * deleted).
2274 */
2275 static void
2276py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
2277{
2278 if (curwin->w_cursor.lnum >= lo)
2279 {
2280 /* Adjust the cursor position if it's in/after the changed
2281 * lines. */
2282 if (curwin->w_cursor.lnum >= hi)
2283 {
2284 curwin->w_cursor.lnum += extra;
2285 check_cursor_col();
2286 }
2287 else if (extra < 0)
2288 {
2289 curwin->w_cursor.lnum = lo;
2290 check_cursor();
2291 }
2292 else
2293 check_cursor_col();
2294 changed_cline_bef_curs();
2295 }
2296 invalidate_botline();
2297}
2298
Bram Moolenaar19e60942011-06-19 00:27:51 +02002299/*
2300 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002301 * in Vim format (1-based). The replacement line is given as
2302 * a Python string object. The object is checked for validity
2303 * and correct format. Errors are returned as a value of FAIL.
2304 * The return value is OK on success.
2305 * If OK is returned and len_change is not NULL, *len_change
2306 * is set to the change in the buffer length.
2307 */
2308 static int
2309SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
2310{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002311 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002312 * There are three cases:
2313 * 1. NULL, or None - this is a deletion.
2314 * 2. A string - this is a replacement.
2315 * 3. Anything else - this is an error.
2316 */
2317 if (line == Py_None || line == NULL)
2318 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002319 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002320
2321 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002322 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002323
2324 if (u_savedel((linenr_T)n, 1L) == FAIL)
2325 PyErr_SetVim(_("cannot save undo information"));
2326 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2327 PyErr_SetVim(_("cannot delete line"));
2328 else
2329 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02002330 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002331 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
2332 deleted_lines_mark((linenr_T)n, 1L);
2333 }
2334
Bram Moolenaar105bc352013-05-17 16:03:57 +02002335 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002336
2337 if (PyErr_Occurred() || VimErrorCheck())
2338 return FAIL;
2339
2340 if (len_change)
2341 *len_change = -1;
2342
2343 return OK;
2344 }
2345 else if (PyString_Check(line))
2346 {
2347 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002348 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002349
2350 if (save == NULL)
2351 return FAIL;
2352
2353 /* We do not need to free "save" if ml_replace() consumes it. */
2354 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002355 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002356
2357 if (u_savesub((linenr_T)n) == FAIL)
2358 {
2359 PyErr_SetVim(_("cannot save undo information"));
2360 vim_free(save);
2361 }
2362 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2363 {
2364 PyErr_SetVim(_("cannot replace line"));
2365 vim_free(save);
2366 }
2367 else
2368 changed_bytes((linenr_T)n, 0);
2369
Bram Moolenaar105bc352013-05-17 16:03:57 +02002370 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002371
2372 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02002373 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002374 check_cursor_col();
2375
2376 if (PyErr_Occurred() || VimErrorCheck())
2377 return FAIL;
2378
2379 if (len_change)
2380 *len_change = 0;
2381
2382 return OK;
2383 }
2384 else
2385 {
2386 PyErr_BadArgument();
2387 return FAIL;
2388 }
2389}
2390
Bram Moolenaar19e60942011-06-19 00:27:51 +02002391/* Replace a range of lines in the specified buffer. The line numbers are in
2392 * Vim format (1-based). The range is from lo up to, but not including, hi.
2393 * The replacement lines are given as a Python list of string objects. The
2394 * list is checked for validity and correct format. Errors are returned as a
2395 * value of FAIL. The return value is OK on success.
2396 * If OK is returned and len_change is not NULL, *len_change
2397 * is set to the change in the buffer length.
2398 */
2399 static int
2400SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
2401{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002402 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02002403 * There are three cases:
2404 * 1. NULL, or None - this is a deletion.
2405 * 2. A list - this is a replacement.
2406 * 3. Anything else - this is an error.
2407 */
2408 if (list == Py_None || list == NULL)
2409 {
2410 PyInt i;
2411 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002412 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02002413
2414 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002415 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002416
2417 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2418 PyErr_SetVim(_("cannot save undo information"));
2419 else
2420 {
2421 for (i = 0; i < n; ++i)
2422 {
2423 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2424 {
2425 PyErr_SetVim(_("cannot delete line"));
2426 break;
2427 }
2428 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02002429 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002430 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
2431 deleted_lines_mark((linenr_T)lo, (long)i);
2432 }
2433
Bram Moolenaar105bc352013-05-17 16:03:57 +02002434 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002435
2436 if (PyErr_Occurred() || VimErrorCheck())
2437 return FAIL;
2438
2439 if (len_change)
2440 *len_change = -n;
2441
2442 return OK;
2443 }
2444 else if (PyList_Check(list))
2445 {
2446 PyInt i;
2447 PyInt new_len = PyList_Size(list);
2448 PyInt old_len = hi - lo;
2449 PyInt extra = 0; /* lines added to text, can be negative */
2450 char **array;
2451 buf_T *savebuf;
2452
2453 if (new_len == 0) /* avoid allocating zero bytes */
2454 array = NULL;
2455 else
2456 {
2457 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2458 if (array == NULL)
2459 {
2460 PyErr_NoMemory();
2461 return FAIL;
2462 }
2463 }
2464
2465 for (i = 0; i < new_len; ++i)
2466 {
2467 PyObject *line = PyList_GetItem(list, i);
2468
2469 array[i] = StringToLine(line);
2470 if (array[i] == NULL)
2471 {
2472 while (i)
2473 vim_free(array[--i]);
2474 vim_free(array);
2475 return FAIL;
2476 }
2477 }
2478
Bram Moolenaar19e60942011-06-19 00:27:51 +02002479 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002480
2481 // START of region without "return". Must call restore_buffer()!
2482 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002483
2484 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2485 PyErr_SetVim(_("cannot save undo information"));
2486
2487 /* If the size of the range is reducing (ie, new_len < old_len) we
2488 * need to delete some old_len. We do this at the start, by
2489 * repeatedly deleting line "lo".
2490 */
2491 if (!PyErr_Occurred())
2492 {
2493 for (i = 0; i < old_len - new_len; ++i)
2494 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2495 {
2496 PyErr_SetVim(_("cannot delete line"));
2497 break;
2498 }
2499 extra -= i;
2500 }
2501
2502 /* For as long as possible, replace the existing old_len with the
2503 * new old_len. This is a more efficient operation, as it requires
2504 * less memory allocation and freeing.
2505 */
2506 if (!PyErr_Occurred())
2507 {
2508 for (i = 0; i < old_len && i < new_len; ++i)
2509 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2510 == FAIL)
2511 {
2512 PyErr_SetVim(_("cannot replace line"));
2513 break;
2514 }
2515 }
2516 else
2517 i = 0;
2518
2519 /* Now we may need to insert the remaining new old_len. If we do, we
2520 * must free the strings as we finish with them (we can't pass the
2521 * responsibility to vim in this case).
2522 */
2523 if (!PyErr_Occurred())
2524 {
2525 while (i < new_len)
2526 {
2527 if (ml_append((linenr_T)(lo + i - 1),
2528 (char_u *)array[i], 0, FALSE) == FAIL)
2529 {
2530 PyErr_SetVim(_("cannot insert line"));
2531 break;
2532 }
2533 vim_free(array[i]);
2534 ++i;
2535 ++extra;
2536 }
2537 }
2538
2539 /* Free any left-over old_len, as a result of an error */
2540 while (i < new_len)
2541 {
2542 vim_free(array[i]);
2543 ++i;
2544 }
2545
2546 /* Free the array of old_len. All of its contents have now
2547 * been dealt with (either freed, or the responsibility passed
2548 * to vim.
2549 */
2550 vim_free(array);
2551
2552 /* Adjust marks. Invalidate any which lie in the
2553 * changed range, and move any in the remainder of the buffer.
2554 */
2555 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2556 (long)MAXLNUM, (long)extra);
2557 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2558
Bram Moolenaar105bc352013-05-17 16:03:57 +02002559 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002560 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
2561
Bram Moolenaar105bc352013-05-17 16:03:57 +02002562 // END of region without "return".
2563 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02002564
2565 if (PyErr_Occurred() || VimErrorCheck())
2566 return FAIL;
2567
2568 if (len_change)
2569 *len_change = new_len - old_len;
2570
2571 return OK;
2572 }
2573 else
2574 {
2575 PyErr_BadArgument();
2576 return FAIL;
2577 }
2578}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002579
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002580/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002581 * The line number is in Vim format (1-based). The lines to be inserted are
2582 * given as a Python list of string objects or as a single string. The lines
2583 * to be added are checked for validity and correct format. Errors are
2584 * returned as a value of FAIL. The return value is OK on success.
2585 * If OK is returned and len_change is not NULL, *len_change
2586 * is set to the change in the buffer length.
2587 */
2588 static int
2589InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
2590{
2591 /* First of all, we check the type of the supplied Python object.
2592 * It must be a string or a list, or the call is in error.
2593 */
2594 if (PyString_Check(lines))
2595 {
2596 char *str = StringToLine(lines);
2597 buf_T *savebuf;
2598
2599 if (str == NULL)
2600 return FAIL;
2601
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002602 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002603 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002604
2605 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2606 PyErr_SetVim(_("cannot save undo information"));
2607 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2608 PyErr_SetVim(_("cannot insert line"));
2609 else
2610 appended_lines_mark((linenr_T)n, 1L);
2611
2612 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002613 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002614 update_screen(VALID);
2615
2616 if (PyErr_Occurred() || VimErrorCheck())
2617 return FAIL;
2618
2619 if (len_change)
2620 *len_change = 1;
2621
2622 return OK;
2623 }
2624 else if (PyList_Check(lines))
2625 {
2626 PyInt i;
2627 PyInt size = PyList_Size(lines);
2628 char **array;
2629 buf_T *savebuf;
2630
2631 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2632 if (array == NULL)
2633 {
2634 PyErr_NoMemory();
2635 return FAIL;
2636 }
2637
2638 for (i = 0; i < size; ++i)
2639 {
2640 PyObject *line = PyList_GetItem(lines, i);
2641 array[i] = StringToLine(line);
2642
2643 if (array[i] == NULL)
2644 {
2645 while (i)
2646 vim_free(array[--i]);
2647 vim_free(array);
2648 return FAIL;
2649 }
2650 }
2651
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002652 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02002653 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002654
2655 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2656 PyErr_SetVim(_("cannot save undo information"));
2657 else
2658 {
2659 for (i = 0; i < size; ++i)
2660 {
2661 if (ml_append((linenr_T)(n + i),
2662 (char_u *)array[i], 0, FALSE) == FAIL)
2663 {
2664 PyErr_SetVim(_("cannot insert line"));
2665
2666 /* Free the rest of the lines */
2667 while (i < size)
2668 vim_free(array[i++]);
2669
2670 break;
2671 }
2672 vim_free(array[i]);
2673 }
2674 if (i > 0)
2675 appended_lines_mark((linenr_T)n, (long)i);
2676 }
2677
2678 /* Free the array of lines. All of its contents have now
2679 * been freed.
2680 */
2681 vim_free(array);
2682
Bram Moolenaar105bc352013-05-17 16:03:57 +02002683 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002684 update_screen(VALID);
2685
2686 if (PyErr_Occurred() || VimErrorCheck())
2687 return FAIL;
2688
2689 if (len_change)
2690 *len_change = size;
2691
2692 return OK;
2693 }
2694 else
2695 {
2696 PyErr_BadArgument();
2697 return FAIL;
2698 }
2699}
2700
2701/*
2702 * Common routines for buffers and line ranges
2703 * -------------------------------------------
2704 */
2705
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002706typedef struct
2707{
2708 PyObject_HEAD
2709 buf_T *buf;
2710} BufferObject;
2711
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002712 static int
2713CheckBuffer(BufferObject *this)
2714{
2715 if (this->buf == INVALID_BUFFER_VALUE)
2716 {
2717 PyErr_SetVim(_("attempt to refer to deleted buffer"));
2718 return -1;
2719 }
2720
2721 return 0;
2722}
2723
2724 static PyObject *
2725RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
2726{
2727 if (CheckBuffer(self))
2728 return NULL;
2729
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002730 if (end == -1)
2731 end = self->buf->b_ml.ml_line_count;
2732
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002733 if (n < 0)
2734 n += end - start + 1;
2735
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002736 if (n < 0 || n > end - start)
2737 {
2738 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2739 return NULL;
2740 }
2741
2742 return GetBufferLine(self->buf, n+start);
2743}
2744
2745 static PyObject *
2746RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
2747{
2748 PyInt size;
2749
2750 if (CheckBuffer(self))
2751 return NULL;
2752
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002753 if (end == -1)
2754 end = self->buf->b_ml.ml_line_count;
2755
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002756 size = end - start + 1;
2757
2758 if (lo < 0)
2759 lo = 0;
2760 else if (lo > size)
2761 lo = size;
2762 if (hi < 0)
2763 hi = 0;
2764 if (hi < lo)
2765 hi = lo;
2766 else if (hi > size)
2767 hi = size;
2768
2769 return GetBufferLineList(self->buf, lo+start, hi+start);
2770}
2771
2772 static PyInt
2773RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2774{
2775 PyInt len_change;
2776
2777 if (CheckBuffer(self))
2778 return -1;
2779
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002780 if (end == -1)
2781 end = self->buf->b_ml.ml_line_count;
2782
Bram Moolenaarbd80f352013-05-12 21:16:23 +02002783 if (n < 0)
2784 n += end - start + 1;
2785
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002786 if (n < 0 || n > end - start)
2787 {
2788 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
2789 return -1;
2790 }
2791
2792 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
2793 return -1;
2794
2795 if (new_end)
2796 *new_end = end + len_change;
2797
2798 return 0;
2799}
2800
Bram Moolenaar19e60942011-06-19 00:27:51 +02002801 static PyInt
2802RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
2803{
2804 PyInt size;
2805 PyInt len_change;
2806
2807 /* Self must be a valid buffer */
2808 if (CheckBuffer(self))
2809 return -1;
2810
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002811 if (end == -1)
2812 end = self->buf->b_ml.ml_line_count;
2813
Bram Moolenaar19e60942011-06-19 00:27:51 +02002814 /* Sort out the slice range */
2815 size = end - start + 1;
2816
2817 if (lo < 0)
2818 lo = 0;
2819 else if (lo > size)
2820 lo = size;
2821 if (hi < 0)
2822 hi = 0;
2823 if (hi < lo)
2824 hi = lo;
2825 else if (hi > size)
2826 hi = size;
2827
2828 if (SetBufferLineList(self->buf, lo + start, hi + start,
2829 val, &len_change) == FAIL)
2830 return -1;
2831
2832 if (new_end)
2833 *new_end = end + len_change;
2834
2835 return 0;
2836}
2837
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002838
2839 static PyObject *
2840RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
2841{
2842 PyObject *lines;
2843 PyInt len_change;
2844 PyInt max;
2845 PyInt n;
2846
2847 if (CheckBuffer(self))
2848 return NULL;
2849
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02002850 if (end == -1)
2851 end = self->buf->b_ml.ml_line_count;
2852
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002853 max = n = end - start + 1;
2854
2855 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
2856 return NULL;
2857
2858 if (n < 0 || n > max)
2859 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002860 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002861 return NULL;
2862 }
2863
2864 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
2865 return NULL;
2866
2867 if (new_end)
2868 *new_end = end + len_change;
2869
2870 Py_INCREF(Py_None);
2871 return Py_None;
2872}
2873
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002874/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002875 */
2876
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002877static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002878static PySequenceMethods RangeAsSeq;
2879static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002880
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002881typedef struct
2882{
2883 PyObject_HEAD
2884 BufferObject *buf;
2885 PyInt start;
2886 PyInt end;
2887} RangeObject;
2888
2889 static PyObject *
2890RangeNew(buf_T *buf, PyInt start, PyInt end)
2891{
2892 BufferObject *bufr;
2893 RangeObject *self;
2894 self = PyObject_NEW(RangeObject, &RangeType);
2895 if (self == NULL)
2896 return NULL;
2897
2898 bufr = (BufferObject *)BufferNew(buf);
2899 if (bufr == NULL)
2900 {
2901 Py_DECREF(self);
2902 return NULL;
2903 }
2904 Py_INCREF(bufr);
2905
2906 self->buf = bufr;
2907 self->start = start;
2908 self->end = end;
2909
2910 return (PyObject *)(self);
2911}
2912
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002913 static void
2914RangeDestructor(PyObject *self)
2915{
2916 Py_DECREF(((RangeObject *)(self))->buf);
2917 DESTRUCTOR_FINISH(self);
2918}
2919
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002920 static PyInt
2921RangeLength(PyObject *self)
2922{
2923 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
2924 if (CheckBuffer(((RangeObject *)(self))->buf))
2925 return -1; /* ??? */
2926
2927 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
2928}
2929
2930 static PyObject *
2931RangeItem(PyObject *self, PyInt n)
2932{
2933 return RBItem(((RangeObject *)(self))->buf, n,
2934 ((RangeObject *)(self))->start,
2935 ((RangeObject *)(self))->end);
2936}
2937
2938 static PyObject *
2939RangeSlice(PyObject *self, PyInt lo, PyInt hi)
2940{
2941 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
2942 ((RangeObject *)(self))->start,
2943 ((RangeObject *)(self))->end);
2944}
2945
2946 static PyObject *
2947RangeAppend(PyObject *self, PyObject *args)
2948{
2949 return RBAppend(((RangeObject *)(self))->buf, args,
2950 ((RangeObject *)(self))->start,
2951 ((RangeObject *)(self))->end,
2952 &((RangeObject *)(self))->end);
2953}
2954
2955 static PyObject *
2956RangeRepr(PyObject *self)
2957{
2958 static char repr[100];
2959 RangeObject *this = (RangeObject *)(self);
2960
2961 if (this->buf->buf == INVALID_BUFFER_VALUE)
2962 {
2963 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
2964 (self));
2965 return PyString_FromString(repr);
2966 }
2967 else
2968 {
2969 char *name = (char *)this->buf->buf->b_fname;
2970 int len;
2971
2972 if (name == NULL)
2973 name = "";
2974 len = (int)strlen(name);
2975
2976 if (len > 45)
2977 name = name + (45 - len);
2978
2979 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
2980 len > 45 ? "..." : "", name,
2981 this->start, this->end);
2982
2983 return PyString_FromString(repr);
2984 }
2985}
2986
2987static struct PyMethodDef RangeMethods[] = {
2988 /* name, function, calling, documentation */
2989 {"append", RangeAppend, 1, "Append data to the Vim range" },
2990 { NULL, NULL, 0, NULL }
2991};
2992
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002993static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002994static PySequenceMethods BufferAsSeq;
2995static PyMappingMethods BufferAsMapping;
2996
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002997 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02002998BufferNew(buf_T *buf)
2999{
3000 /* We need to handle deletion of buffers underneath us.
3001 * If we add a "b_python*_ref" field to the buf_T structure,
3002 * then we can get at it in buf_freeall() in vim. We then
3003 * need to create only ONE Python object per buffer - if
3004 * we try to create a second, just INCREF the existing one
3005 * and return it. The (single) Python object referring to
3006 * the buffer is stored in "b_python*_ref".
3007 * Question: what to do on a buf_freeall(). We'll probably
3008 * have to either delete the Python object (DECREF it to
3009 * zero - a bad idea, as it leaves dangling refs!) or
3010 * set the buf_T * value to an invalid value (-1?), which
3011 * means we need checks in all access functions... Bah.
3012 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003013 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003014 * b_python_ref and b_python3_ref fields respectively.
3015 */
3016
3017 BufferObject *self;
3018
3019 if (BUF_PYTHON_REF(buf) != NULL)
3020 {
3021 self = BUF_PYTHON_REF(buf);
3022 Py_INCREF(self);
3023 }
3024 else
3025 {
3026 self = PyObject_NEW(BufferObject, &BufferType);
3027 if (self == NULL)
3028 return NULL;
3029 self->buf = buf;
3030 BUF_PYTHON_REF(buf) = self;
3031 }
3032
3033 return (PyObject *)(self);
3034}
3035
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003036 static void
3037BufferDestructor(PyObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003038{
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003039 BufferObject *this = (BufferObject *)(self);
3040
3041 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
3042 BUF_PYTHON_REF(this->buf) = NULL;
3043
3044 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003045}
3046
Bram Moolenaar971db462013-05-12 18:44:48 +02003047 static PyInt
3048BufferLength(PyObject *self)
3049{
3050 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3051 if (CheckBuffer((BufferObject *)(self)))
3052 return -1; /* ??? */
3053
3054 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
3055}
3056
3057 static PyObject *
3058BufferItem(PyObject *self, PyInt n)
3059{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003060 return RBItem((BufferObject *)(self), n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003061}
3062
3063 static PyObject *
3064BufferSlice(PyObject *self, PyInt lo, PyInt hi)
3065{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003066 return RBSlice((BufferObject *)(self), lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02003067}
3068
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003069 static PyObject *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003070BufferAttr(BufferObject *this, char *name)
3071{
3072 if (strcmp(name, "name") == 0)
3073 return Py_BuildValue("s", this->buf->b_ffname);
3074 else if (strcmp(name, "number") == 0)
3075 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
3076 else if (strcmp(name, "vars") == 0)
3077 return DictionaryNew(this->buf->b_vars);
3078 else if (strcmp(name, "options") == 0)
3079 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer,
3080 (PyObject *) this);
3081 else if (strcmp(name,"__members__") == 0)
3082 return Py_BuildValue("[ssss]", "name", "number", "vars", "options");
3083 else
3084 return NULL;
3085}
3086
3087 static PyObject *
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003088BufferAppend(PyObject *self, PyObject *args)
3089{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003090 return RBAppend((BufferObject *)(self), args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003091}
3092
3093 static PyObject *
3094BufferMark(PyObject *self, PyObject *args)
3095{
3096 pos_T *posp;
3097 char *pmark;
3098 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02003099 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003100
3101 if (CheckBuffer((BufferObject *)(self)))
3102 return NULL;
3103
3104 if (!PyArg_ParseTuple(args, "s", &pmark))
3105 return NULL;
3106 mark = *pmark;
3107
Bram Moolenaar105bc352013-05-17 16:03:57 +02003108 switch_buffer(&savebuf, ((BufferObject *)(self))->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003109 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003110 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003111
3112 if (posp == NULL)
3113 {
3114 PyErr_SetVim(_("invalid mark name"));
3115 return NULL;
3116 }
3117
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003118 /* Check for keyboard interrupt */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003119 if (VimErrorCheck())
3120 return NULL;
3121
3122 if (posp->lnum <= 0)
3123 {
3124 /* Or raise an error? */
3125 Py_INCREF(Py_None);
3126 return Py_None;
3127 }
3128
3129 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3130}
3131
3132 static PyObject *
3133BufferRange(PyObject *self, PyObject *args)
3134{
3135 PyInt start;
3136 PyInt end;
3137
3138 if (CheckBuffer((BufferObject *)(self)))
3139 return NULL;
3140
3141 if (!PyArg_ParseTuple(args, "nn", &start, &end))
3142 return NULL;
3143
3144 return RangeNew(((BufferObject *)(self))->buf, start, end);
3145}
3146
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003147 static PyObject *
3148BufferRepr(PyObject *self)
3149{
3150 static char repr[100];
3151 BufferObject *this = (BufferObject *)(self);
3152
3153 if (this->buf == INVALID_BUFFER_VALUE)
3154 {
3155 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
3156 return PyString_FromString(repr);
3157 }
3158 else
3159 {
3160 char *name = (char *)this->buf->b_fname;
3161 PyInt len;
3162
3163 if (name == NULL)
3164 name = "";
3165 len = strlen(name);
3166
3167 if (len > 35)
3168 name = name + (35 - len);
3169
3170 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
3171
3172 return PyString_FromString(repr);
3173 }
3174}
3175
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003176static struct PyMethodDef BufferMethods[] = {
3177 /* name, function, calling, documentation */
3178 {"append", BufferAppend, 1, "Append data to Vim buffer" },
3179 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
3180 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaar7f85d292012-02-04 20:17:26 +01003181#if PY_VERSION_HEX >= 0x03000000
3182 {"__dir__", BufferDir, 4, "List its attributes" },
3183#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003184 { NULL, NULL, 0, NULL }
3185};
3186
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187/*
3188 * Buffer list object - Implementation
3189 */
3190
3191static PyTypeObject BufMapType;
3192
3193typedef struct
3194{
3195 PyObject_HEAD
3196} BufMapObject;
3197
3198 static PyInt
3199BufMapLength(PyObject *self UNUSED)
3200{
3201 buf_T *b = firstbuf;
3202 PyInt n = 0;
3203
3204 while (b)
3205 {
3206 ++n;
3207 b = b->b_next;
3208 }
3209
3210 return n;
3211}
3212
3213 static PyObject *
3214BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
3215{
3216 buf_T *b;
3217 int bnr;
3218
3219#if PY_MAJOR_VERSION < 3
3220 if (PyInt_Check(keyObject))
3221 bnr = PyInt_AsLong(keyObject);
3222 else
3223#endif
3224 if (PyLong_Check(keyObject))
3225 bnr = PyLong_AsLong(keyObject);
3226 else
3227 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003228 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003229 return NULL;
3230 }
3231
3232 b = buflist_findnr(bnr);
3233
3234 if (b)
3235 return BufferNew(b);
3236 else
3237 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003238 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003239 return NULL;
3240 }
3241}
3242
3243 static void
3244BufMapIterDestruct(PyObject *buffer)
3245{
3246 /* Iteration was stopped before all buffers were processed */
3247 if (buffer)
3248 {
3249 Py_DECREF(buffer);
3250 }
3251}
3252
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003253 static int
3254BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
3255{
3256 Py_VISIT(buffer);
3257 return 0;
3258}
3259
3260 static int
3261BufMapIterClear(PyObject **buffer)
3262{
3263 Py_CLEAR(*buffer);
3264 return 0;
3265}
3266
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003267 static PyObject *
3268BufMapIterNext(PyObject **buffer)
3269{
3270 PyObject *next;
3271 PyObject *r;
3272
3273 if (!*buffer)
3274 return NULL;
3275
3276 r = *buffer;
3277
3278 if (CheckBuffer((BufferObject *)(r)))
3279 {
3280 *buffer = NULL;
3281 return NULL;
3282 }
3283
3284 if (!((BufferObject *)(r))->buf->b_next)
3285 next = NULL;
3286 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
3287 return NULL;
3288 *buffer = next;
3289 /* Do not increment reference: we no longer hold it (decref), but whoever on
3290 * other side will hold (incref). Decref+incref = nothing.
3291 */
3292 return r;
3293}
3294
3295 static PyObject *
3296BufMapIter(PyObject *self UNUSED)
3297{
3298 PyObject *buffer;
3299
3300 buffer = BufferNew(firstbuf);
3301 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003302 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
3303 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003304}
3305
3306static PyMappingMethods BufMapAsMapping = {
3307 (lenfunc) BufMapLength,
3308 (binaryfunc) BufMapItem,
3309 (objobjargproc) 0,
3310};
3311
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003312/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003313 */
3314
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003315 static PyObject *
3316CurrentGetattr(PyObject *self UNUSED, char *name)
3317{
3318 if (strcmp(name, "buffer") == 0)
3319 return (PyObject *)BufferNew(curbuf);
3320 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003321 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003322 else if (strcmp(name, "tabpage") == 0)
3323 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003324 else if (strcmp(name, "line") == 0)
3325 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3326 else if (strcmp(name, "range") == 0)
3327 return RangeNew(curbuf, RangeStart, RangeEnd);
3328 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003329 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range",
3330 "tabpage");
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003331 else
3332 {
3333 PyErr_SetString(PyExc_AttributeError, name);
3334 return NULL;
3335 }
3336}
3337
3338 static int
3339CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
3340{
3341 if (strcmp(name, "line") == 0)
3342 {
3343 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
3344 return -1;
3345
3346 return 0;
3347 }
Bram Moolenaare7614592013-05-15 15:51:08 +02003348 else if (strcmp(name, "buffer") == 0)
3349 {
3350 int count;
3351
3352 if (value->ob_type != &BufferType)
3353 {
3354 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object"));
3355 return -1;
3356 }
3357
3358 if (CheckBuffer((BufferObject *)(value)))
3359 return -1;
3360 count = ((BufferObject *)(value))->buf->b_fnum;
3361
3362 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
3363 {
3364 PyErr_SetVim(_("failed to switch to given buffer"));
3365 return -1;
3366 }
3367
3368 return 0;
3369 }
3370 else if (strcmp(name, "window") == 0)
3371 {
3372 int count;
3373
3374 if (value->ob_type != &WindowType)
3375 {
3376 PyErr_SetString(PyExc_TypeError, _("expected vim.window object"));
3377 return -1;
3378 }
3379
3380 if (CheckWindow((WindowObject *)(value)))
3381 return -1;
3382 count = get_win_number(((WindowObject *)(value))->win, firstwin);
3383
3384 if (!count)
3385 {
3386 PyErr_SetString(PyExc_ValueError,
3387 _("failed to find window in the current tab page"));
3388 return -1;
3389 }
3390
3391 win_goto(((WindowObject *)(value))->win);
3392 if (((WindowObject *)(value))->win != curwin)
3393 {
3394 PyErr_SetString(PyExc_RuntimeError,
3395 _("did not switch to the specified window"));
3396 return -1;
3397 }
3398
3399 return 0;
3400 }
3401 else if (strcmp(name, "tabpage") == 0)
3402 {
3403 if (value->ob_type != &TabPageType)
3404 {
3405 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object"));
3406 return -1;
3407 }
3408
3409 if (CheckTabPage((TabPageObject *)(value)))
3410 return -1;
3411
3412 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
3413 if (((TabPageObject *)(value))->tab != curtab)
3414 {
3415 PyErr_SetString(PyExc_RuntimeError,
3416 _("did not switch to the specified tab page"));
3417 return -1;
3418 }
3419
3420 return 0;
3421 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003422 else
3423 {
3424 PyErr_SetString(PyExc_AttributeError, name);
3425 return -1;
3426 }
3427}
3428
Bram Moolenaardb913952012-06-29 12:54:53 +02003429 static void
3430set_ref_in_py(const int copyID)
3431{
3432 pylinkedlist_T *cur;
3433 dict_T *dd;
3434 list_T *ll;
3435
3436 if (lastdict != NULL)
3437 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
3438 {
3439 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
3440 if (dd->dv_copyID != copyID)
3441 {
3442 dd->dv_copyID = copyID;
3443 set_ref_in_ht(&dd->dv_hashtab, copyID);
3444 }
3445 }
3446
3447 if (lastlist != NULL)
3448 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
3449 {
3450 ll = ((ListObject *) (cur->pll_obj))->list;
3451 if (ll->lv_copyID != copyID)
3452 {
3453 ll->lv_copyID = copyID;
3454 set_ref_in_list(ll, copyID);
3455 }
3456 }
3457}
3458
3459 static int
3460set_string_copy(char_u *str, typval_T *tv)
3461{
3462 tv->vval.v_string = vim_strsave(str);
3463 if (tv->vval.v_string == NULL)
3464 {
3465 PyErr_NoMemory();
3466 return -1;
3467 }
3468 return 0;
3469}
3470
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003471 static int
3472pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3473{
3474 dict_T *d;
3475 char_u *key;
3476 dictitem_T *di;
3477 PyObject *keyObject;
3478 PyObject *valObject;
3479 Py_ssize_t iter = 0;
3480
3481 d = dict_alloc();
3482 if (d == NULL)
3483 {
3484 PyErr_NoMemory();
3485 return -1;
3486 }
3487
3488 tv->v_type = VAR_DICT;
3489 tv->vval.v_dict = d;
3490
3491 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
3492 {
3493 DICTKEY_DECL
3494
3495 if (keyObject == NULL)
3496 return -1;
3497 if (valObject == NULL)
3498 return -1;
3499
3500 DICTKEY_GET_NOTEMPTY(-1)
3501
3502 di = dictitem_alloc(key);
3503
3504 DICTKEY_UNREF
3505
3506 if (di == NULL)
3507 {
3508 PyErr_NoMemory();
3509 return -1;
3510 }
3511 di->di_tv.v_lock = 0;
3512
3513 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3514 {
3515 vim_free(di);
3516 return -1;
3517 }
3518 if (dict_add(d, di) == FAIL)
3519 {
3520 vim_free(di);
3521 PyErr_SetVim(_("failed to add key to dictionary"));
3522 return -1;
3523 }
3524 }
3525 return 0;
3526}
3527
3528 static int
3529pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3530{
3531 dict_T *d;
3532 char_u *key;
3533 dictitem_T *di;
3534 PyObject *list;
3535 PyObject *litem;
3536 PyObject *keyObject;
3537 PyObject *valObject;
3538 Py_ssize_t lsize;
3539
3540 d = dict_alloc();
3541 if (d == NULL)
3542 {
3543 PyErr_NoMemory();
3544 return -1;
3545 }
3546
3547 tv->v_type = VAR_DICT;
3548 tv->vval.v_dict = d;
3549
3550 list = PyMapping_Items(obj);
3551 if (list == NULL)
3552 return -1;
3553 lsize = PyList_Size(list);
3554 while (lsize--)
3555 {
3556 DICTKEY_DECL
3557
3558 litem = PyList_GetItem(list, lsize);
3559 if (litem == NULL)
3560 {
3561 Py_DECREF(list);
3562 return -1;
3563 }
3564
3565 keyObject = PyTuple_GetItem(litem, 0);
3566 if (keyObject == NULL)
3567 {
3568 Py_DECREF(list);
3569 Py_DECREF(litem);
3570 return -1;
3571 }
3572
3573 DICTKEY_GET_NOTEMPTY(-1)
3574
3575 valObject = PyTuple_GetItem(litem, 1);
3576 if (valObject == NULL)
3577 {
3578 Py_DECREF(list);
3579 Py_DECREF(litem);
3580 return -1;
3581 }
3582
3583 di = dictitem_alloc(key);
3584
3585 DICTKEY_UNREF
3586
3587 if (di == NULL)
3588 {
3589 Py_DECREF(list);
3590 Py_DECREF(litem);
3591 PyErr_NoMemory();
3592 return -1;
3593 }
3594 di->di_tv.v_lock = 0;
3595
3596 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
3597 {
3598 vim_free(di);
3599 Py_DECREF(list);
3600 Py_DECREF(litem);
3601 return -1;
3602 }
3603 if (dict_add(d, di) == FAIL)
3604 {
3605 vim_free(di);
3606 Py_DECREF(list);
3607 Py_DECREF(litem);
3608 PyErr_SetVim(_("failed to add key to dictionary"));
3609 return -1;
3610 }
3611 Py_DECREF(litem);
3612 }
3613 Py_DECREF(list);
3614 return 0;
3615}
3616
3617 static int
3618pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3619{
3620 list_T *l;
3621
3622 l = list_alloc();
3623 if (l == NULL)
3624 {
3625 PyErr_NoMemory();
3626 return -1;
3627 }
3628
3629 tv->v_type = VAR_LIST;
3630 tv->vval.v_list = l;
3631
3632 if (list_py_concat(l, obj, lookupDict) == -1)
3633 return -1;
3634
3635 return 0;
3636}
3637
3638 static int
3639pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3640{
3641 PyObject *iterator = PyObject_GetIter(obj);
3642 PyObject *item;
3643 list_T *l;
3644 listitem_T *li;
3645
3646 l = list_alloc();
3647
3648 if (l == NULL)
3649 {
3650 PyErr_NoMemory();
3651 return -1;
3652 }
3653
3654 tv->vval.v_list = l;
3655 tv->v_type = VAR_LIST;
3656
3657
3658 if (iterator == NULL)
3659 return -1;
3660
3661 while ((item = PyIter_Next(obj)))
3662 {
3663 li = listitem_alloc();
3664 if (li == NULL)
3665 {
3666 PyErr_NoMemory();
3667 return -1;
3668 }
3669 li->li_tv.v_lock = 0;
3670
3671 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
3672 return -1;
3673
3674 list_append(l, li);
3675
3676 Py_DECREF(item);
3677 }
3678
3679 Py_DECREF(iterator);
3680 return 0;
3681}
3682
Bram Moolenaardb913952012-06-29 12:54:53 +02003683typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
3684
3685 static int
3686convert_dl(PyObject *obj, typval_T *tv,
3687 pytotvfunc py_to_tv, PyObject *lookupDict)
3688{
3689 PyObject *capsule;
3690 char hexBuf[sizeof(void *) * 2 + 3];
3691
3692 sprintf(hexBuf, "%p", obj);
3693
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003694# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003695 capsule = PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003696# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003697 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003698# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02003699 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02003700 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003701# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02003702 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02003703# else
3704 capsule = PyCObject_FromVoidPtr(tv, NULL);
3705# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003706 PyDict_SetItemString(lookupDict, hexBuf, capsule);
3707 Py_DECREF(capsule);
3708 if (py_to_tv(obj, tv, lookupDict) == -1)
3709 {
3710 tv->v_type = VAR_UNKNOWN;
3711 return -1;
3712 }
3713 /* As we are not using copy_tv which increments reference count we must
3714 * do it ourself. */
3715 switch(tv->v_type)
3716 {
3717 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
3718 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
3719 }
3720 }
3721 else
3722 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003723 typval_T *v;
3724
3725# ifdef PY_USE_CAPSULE
3726 v = PyCapsule_GetPointer(capsule, NULL);
3727# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02003728 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02003729# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02003730 copy_tv(v, tv);
3731 }
3732 return 0;
3733}
3734
3735 static int
3736ConvertFromPyObject(PyObject *obj, typval_T *tv)
3737{
3738 PyObject *lookup_dict;
3739 int r;
3740
3741 lookup_dict = PyDict_New();
3742 r = _ConvertFromPyObject(obj, tv, lookup_dict);
3743 Py_DECREF(lookup_dict);
3744 return r;
3745}
3746
3747 static int
3748_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
3749{
3750 if (obj->ob_type == &DictionaryType)
3751 {
3752 tv->v_type = VAR_DICT;
3753 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
3754 ++tv->vval.v_dict->dv_refcount;
3755 }
3756 else if (obj->ob_type == &ListType)
3757 {
3758 tv->v_type = VAR_LIST;
3759 tv->vval.v_list = (((ListObject *)(obj))->list);
3760 ++tv->vval.v_list->lv_refcount;
3761 }
3762 else if (obj->ob_type == &FunctionType)
3763 {
3764 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
3765 return -1;
3766
3767 tv->v_type = VAR_FUNC;
3768 func_ref(tv->vval.v_string);
3769 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003770 else if (PyBytes_Check(obj))
3771 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003772 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02003773
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003774 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
3775 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003776 if (result == NULL)
3777 return -1;
3778
3779 if (set_string_copy(result, tv) == -1)
3780 return -1;
3781
3782 tv->v_type = VAR_STRING;
3783 }
3784 else if (PyUnicode_Check(obj))
3785 {
3786 PyObject *bytes;
3787 char_u *result;
3788
Bram Moolenaardb913952012-06-29 12:54:53 +02003789 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
3790 if (bytes == NULL)
3791 return -1;
3792
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02003793 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
3794 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02003795 if (result == NULL)
3796 return -1;
3797
3798 if (set_string_copy(result, tv) == -1)
3799 {
3800 Py_XDECREF(bytes);
3801 return -1;
3802 }
3803 Py_XDECREF(bytes);
3804
3805 tv->v_type = VAR_STRING;
3806 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02003807#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02003808 else if (PyInt_Check(obj))
3809 {
3810 tv->v_type = VAR_NUMBER;
3811 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
3812 }
3813#endif
3814 else if (PyLong_Check(obj))
3815 {
3816 tv->v_type = VAR_NUMBER;
3817 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
3818 }
3819 else if (PyDict_Check(obj))
3820 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
3821#ifdef FEAT_FLOAT
3822 else if (PyFloat_Check(obj))
3823 {
3824 tv->v_type = VAR_FLOAT;
3825 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
3826 }
3827#endif
3828 else if (PyIter_Check(obj))
3829 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
3830 else if (PySequence_Check(obj))
3831 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
3832 else if (PyMapping_Check(obj))
3833 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
3834 else
3835 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003836 PyErr_SetString(PyExc_TypeError,
3837 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02003838 return -1;
3839 }
3840 return 0;
3841}
3842
3843 static PyObject *
3844ConvertToPyObject(typval_T *tv)
3845{
3846 if (tv == NULL)
3847 {
3848 PyErr_SetVim(_("NULL reference passed"));
3849 return NULL;
3850 }
3851 switch (tv->v_type)
3852 {
3853 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003854 return PyBytes_FromString(tv->vval.v_string == NULL
3855 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003856 case VAR_NUMBER:
3857 return PyLong_FromLong((long) tv->vval.v_number);
3858#ifdef FEAT_FLOAT
3859 case VAR_FLOAT:
3860 return PyFloat_FromDouble((double) tv->vval.v_float);
3861#endif
3862 case VAR_LIST:
3863 return ListNew(tv->vval.v_list);
3864 case VAR_DICT:
3865 return DictionaryNew(tv->vval.v_dict);
3866 case VAR_FUNC:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02003867 return FunctionNew(tv->vval.v_string == NULL
3868 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02003869 case VAR_UNKNOWN:
3870 Py_INCREF(Py_None);
3871 return Py_None;
3872 default:
3873 PyErr_SetVim(_("internal error: invalid value type"));
3874 return NULL;
3875 }
3876}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003877
3878typedef struct
3879{
3880 PyObject_HEAD
3881} CurrentObject;
3882static PyTypeObject CurrentType;
3883
3884 static void
3885init_structs(void)
3886{
3887 vim_memset(&OutputType, 0, sizeof(OutputType));
3888 OutputType.tp_name = "vim.message";
3889 OutputType.tp_basicsize = sizeof(OutputObject);
3890 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
3891 OutputType.tp_doc = "vim message object";
3892 OutputType.tp_methods = OutputMethods;
3893#if PY_MAJOR_VERSION >= 3
3894 OutputType.tp_getattro = OutputGetattro;
3895 OutputType.tp_setattro = OutputSetattro;
3896 OutputType.tp_alloc = call_PyType_GenericAlloc;
3897 OutputType.tp_new = call_PyType_GenericNew;
3898 OutputType.tp_free = call_PyObject_Free;
3899#else
3900 OutputType.tp_getattr = OutputGetattr;
3901 OutputType.tp_setattr = OutputSetattr;
3902#endif
3903
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003904 vim_memset(&IterType, 0, sizeof(IterType));
3905 IterType.tp_name = "vim.iter";
3906 IterType.tp_basicsize = sizeof(IterObject);
3907 IterType.tp_flags = Py_TPFLAGS_DEFAULT;
3908 IterType.tp_doc = "generic iterator object";
3909 IterType.tp_iter = IterIter;
3910 IterType.tp_iternext = IterNext;
Bram Moolenaar2cd73622013-05-15 19:07:47 +02003911 IterType.tp_dealloc = IterDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003912 IterType.tp_traverse = IterTraverse;
3913 IterType.tp_clear = IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003914
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003915 vim_memset(&BufferType, 0, sizeof(BufferType));
3916 BufferType.tp_name = "vim.buffer";
3917 BufferType.tp_basicsize = sizeof(BufferType);
3918 BufferType.tp_dealloc = BufferDestructor;
3919 BufferType.tp_repr = BufferRepr;
3920 BufferType.tp_as_sequence = &BufferAsSeq;
3921 BufferType.tp_as_mapping = &BufferAsMapping;
3922 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
3923 BufferType.tp_doc = "vim buffer object";
3924 BufferType.tp_methods = BufferMethods;
3925#if PY_MAJOR_VERSION >= 3
3926 BufferType.tp_getattro = BufferGetattro;
3927 BufferType.tp_alloc = call_PyType_GenericAlloc;
3928 BufferType.tp_new = call_PyType_GenericNew;
3929 BufferType.tp_free = call_PyObject_Free;
3930#else
3931 BufferType.tp_getattr = BufferGetattr;
3932#endif
3933
3934 vim_memset(&WindowType, 0, sizeof(WindowType));
3935 WindowType.tp_name = "vim.window";
3936 WindowType.tp_basicsize = sizeof(WindowObject);
3937 WindowType.tp_dealloc = WindowDestructor;
3938 WindowType.tp_repr = WindowRepr;
3939 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
3940 WindowType.tp_doc = "vim Window object";
3941 WindowType.tp_methods = WindowMethods;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003942 WindowType.tp_traverse = WindowTraverse;
3943 WindowType.tp_clear = WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003944#if PY_MAJOR_VERSION >= 3
3945 WindowType.tp_getattro = WindowGetattro;
3946 WindowType.tp_setattro = WindowSetattro;
3947 WindowType.tp_alloc = call_PyType_GenericAlloc;
3948 WindowType.tp_new = call_PyType_GenericNew;
3949 WindowType.tp_free = call_PyObject_Free;
3950#else
3951 WindowType.tp_getattr = WindowGetattr;
3952 WindowType.tp_setattr = WindowSetattr;
3953#endif
3954
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003955 vim_memset(&TabPageType, 0, sizeof(TabPageType));
3956 TabPageType.tp_name = "vim.tabpage";
3957 TabPageType.tp_basicsize = sizeof(TabPageObject);
3958 TabPageType.tp_dealloc = TabPageDestructor;
3959 TabPageType.tp_repr = TabPageRepr;
3960 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
3961 TabPageType.tp_doc = "vim tab page object";
3962 TabPageType.tp_methods = TabPageMethods;
3963#if PY_MAJOR_VERSION >= 3
3964 TabPageType.tp_getattro = TabPageGetattro;
3965 TabPageType.tp_alloc = call_PyType_GenericAlloc;
3966 TabPageType.tp_new = call_PyType_GenericNew;
3967 TabPageType.tp_free = call_PyObject_Free;
3968#else
3969 TabPageType.tp_getattr = TabPageGetattr;
3970#endif
3971
Bram Moolenaardfa38d42013-05-15 13:38:47 +02003972 vim_memset(&BufMapType, 0, sizeof(BufMapType));
3973 BufMapType.tp_name = "vim.bufferlist";
3974 BufMapType.tp_basicsize = sizeof(BufMapObject);
3975 BufMapType.tp_as_mapping = &BufMapAsMapping;
3976 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003977 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003978 BufferType.tp_doc = "vim buffer list";
3979
3980 vim_memset(&WinListType, 0, sizeof(WinListType));
3981 WinListType.tp_name = "vim.windowlist";
3982 WinListType.tp_basicsize = sizeof(WinListType);
3983 WinListType.tp_as_sequence = &WinListAsSeq;
3984 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
3985 WinListType.tp_doc = "vim window list";
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003986 WinListType.tp_dealloc = WinListDestructor;
3987
3988 vim_memset(&TabListType, 0, sizeof(TabListType));
3989 TabListType.tp_name = "vim.tabpagelist";
3990 TabListType.tp_basicsize = sizeof(TabListType);
3991 TabListType.tp_as_sequence = &TabListAsSeq;
3992 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
3993 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994
3995 vim_memset(&RangeType, 0, sizeof(RangeType));
3996 RangeType.tp_name = "vim.range";
3997 RangeType.tp_basicsize = sizeof(RangeObject);
3998 RangeType.tp_dealloc = RangeDestructor;
3999 RangeType.tp_repr = RangeRepr;
4000 RangeType.tp_as_sequence = &RangeAsSeq;
4001 RangeType.tp_as_mapping = &RangeAsMapping;
4002 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
4003 RangeType.tp_doc = "vim Range object";
4004 RangeType.tp_methods = RangeMethods;
4005#if PY_MAJOR_VERSION >= 3
4006 RangeType.tp_getattro = RangeGetattro;
4007 RangeType.tp_alloc = call_PyType_GenericAlloc;
4008 RangeType.tp_new = call_PyType_GenericNew;
4009 RangeType.tp_free = call_PyObject_Free;
4010#else
4011 RangeType.tp_getattr = RangeGetattr;
4012#endif
4013
4014 vim_memset(&CurrentType, 0, sizeof(CurrentType));
4015 CurrentType.tp_name = "vim.currentdata";
4016 CurrentType.tp_basicsize = sizeof(CurrentObject);
4017 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
4018 CurrentType.tp_doc = "vim current object";
4019#if PY_MAJOR_VERSION >= 3
4020 CurrentType.tp_getattro = CurrentGetattro;
4021 CurrentType.tp_setattro = CurrentSetattro;
4022#else
4023 CurrentType.tp_getattr = CurrentGetattr;
4024 CurrentType.tp_setattr = CurrentSetattr;
4025#endif
4026
4027 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
4028 DictionaryType.tp_name = "vim.dictionary";
4029 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
4030 DictionaryType.tp_dealloc = DictionaryDestructor;
4031 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
4032 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
4033 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
4034 DictionaryType.tp_methods = DictionaryMethods;
4035#if PY_MAJOR_VERSION >= 3
4036 DictionaryType.tp_getattro = DictionaryGetattro;
4037 DictionaryType.tp_setattro = DictionarySetattro;
4038#else
4039 DictionaryType.tp_getattr = DictionaryGetattr;
4040 DictionaryType.tp_setattr = DictionarySetattr;
4041#endif
4042
4043 vim_memset(&ListType, 0, sizeof(ListType));
4044 ListType.tp_name = "vim.list";
4045 ListType.tp_dealloc = ListDestructor;
4046 ListType.tp_basicsize = sizeof(ListObject);
4047 ListType.tp_as_sequence = &ListAsSeq;
4048 ListType.tp_as_mapping = &ListAsMapping;
4049 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
4050 ListType.tp_doc = "list pushing modifications to vim structure";
4051 ListType.tp_methods = ListMethods;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004052 ListType.tp_iter = ListIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004053#if PY_MAJOR_VERSION >= 3
4054 ListType.tp_getattro = ListGetattro;
4055 ListType.tp_setattro = ListSetattro;
4056#else
4057 ListType.tp_getattr = ListGetattr;
4058 ListType.tp_setattr = ListSetattr;
4059#endif
4060
4061 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004062 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004063 FunctionType.tp_basicsize = sizeof(FunctionObject);
4064 FunctionType.tp_dealloc = FunctionDestructor;
4065 FunctionType.tp_call = FunctionCall;
4066 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
4067 FunctionType.tp_doc = "object that calls vim function";
4068 FunctionType.tp_methods = FunctionMethods;
4069#if PY_MAJOR_VERSION >= 3
4070 FunctionType.tp_getattro = FunctionGetattro;
4071#else
4072 FunctionType.tp_getattr = FunctionGetattr;
4073#endif
4074
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004075 vim_memset(&OptionsType, 0, sizeof(OptionsType));
4076 OptionsType.tp_name = "vim.options";
4077 OptionsType.tp_basicsize = sizeof(OptionsObject);
4078 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT;
4079 OptionsType.tp_doc = "object for manipulating options";
4080 OptionsType.tp_as_mapping = &OptionsAsMapping;
4081 OptionsType.tp_dealloc = OptionsDestructor;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004082 OptionsType.tp_traverse = OptionsTraverse;
4083 OptionsType.tp_clear = OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004084
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004085#if PY_MAJOR_VERSION >= 3
4086 vim_memset(&vimmodule, 0, sizeof(vimmodule));
4087 vimmodule.m_name = "vim";
4088 vimmodule.m_doc = "Vim Python interface\n";
4089 vimmodule.m_size = -1;
4090 vimmodule.m_methods = VimMethods;
4091#endif
4092}